- 新增现代 C++ 教程的 Preface 章节,包括英文和中文版本 - 添加 C++ Primer 练习代码 - 新增 Learn C++ 教程的 C++ 开发简介章节 - 添加头文件解析文档 - 更新 mkdocs.yml,包含新教程的目录结构 - 修改项目设置,使用 Python 3.10环境
37 lines
829 B
C++
37 lines
829 B
C++
#include <iostream>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
using namespace std;
|
|
using std::vector;
|
|
|
|
int main() {
|
|
vector<int> v;
|
|
int w;
|
|
cout << "请输入数字" << endl;
|
|
while (cin >> w) {
|
|
v.push_back(w);
|
|
}
|
|
if (v.size() == 0) {
|
|
cout << "没有任何元素" <<endl;
|
|
return -1;
|
|
}
|
|
cout << "相邻一组数的和为:" <<endl;
|
|
for(decltype((v.size())) i = 0; i < v.size() - 1; i += 2)
|
|
cout << v[i] + v[i + 1] << " ";
|
|
|
|
if (v.size() % 2 != 0)
|
|
cout << v[v.size() - 1];
|
|
cout <<endl;
|
|
|
|
cout << "首尾两项数的和为:" <<endl;
|
|
decltype(v.size()) i = 0, j = v.size() - 1;
|
|
while (i < j) {
|
|
cout << v[i] +v[j] << " ";
|
|
++i,--j;
|
|
}
|
|
if (v.size() % 2 != 0)
|
|
cout << v[v.size() / 2];
|
|
cout <<endl;
|
|
return 0;
|
|
} |