#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(){
vector<int> vec;
int num;
cout << "please input the numbers:" << endl;
while (cin >> num){
vec.push_back(num);
}
int length = vec.size();
if (length == 0){
cout << "there is no number!" << endl;
return 0;
}
for (int j = 0; j < length - 1; j += 2){
cout << vec[j] + vec[j + 1] << " ";
}
cout << endl;
if (length % 2 != 0){
cout << "the last number " << vec[length - 1]<<" can't be added to other" << endl;
}
for (int j = 0; j < length / 2;j++){
cout << vec[j] + vec[length - 1 - j] << " ";
}
cout << endl;
if (length % 2 != 0){
cout << "the mid number " << vec[length / 2] << " can't be added to other" << endl;
}
cout << endl;
return 0;
}
C++ primer P94 练习3.20
最新推荐文章于 2024-03-28 11:19:16 发布
本文介绍了一个使用C++实现的简单程序,该程序读取一系列整数并将其存储在std::vector中。然后程序以两种方式显示这些整数的配对求和结果:相邻元素配对求和及首尾元素配对求和,并处理了元素数量为奇数的情况。
271

被折叠的 条评论
为什么被折叠?



