C++primer习题3.13

计算临界元素的和并输出
#include <iostream> #include <vector> using namespace std; int main() { vector<int> ivec; int i = 0; while(cin >> i) ivec.push_back(i); size_t len = ivec.size(), j = 0; for (; j != len-1; ++j) { cout << ivec[j]+ivec[j+1] << " "; } if ((j+1)%2) { cout << "The last num is not calculated his value"<<endl; } }

计算头尾元素两辆配对

#include <iostream>
#include <vector>
using namespace std;

int main()
{
	vector<int> ivec;
	int i = 0;
	while(cin >> i)
		ivec.push_back(i);
	size_t len = ivec.size(), j = 0;
	for (; j != len/2; ++j)
	{
	   cout << ivec[j]+ivec[len-j-1] << " ";
	}
	if ((j+1)%2)
	{
		cout << "no match" << endl;
	}
}

posted @ 2011-06-08 09:27  hailong  阅读(355)  评论(0编辑  收藏  举报