計算首行兩數相加
#include <iostream>11 #include <vector> using namespace std; int main() { vector<int> ivec; int num; while (cin >> num) { ivec.push_back(num); } int count = ivec.size(); for (int first = 0, last = count - 1; first <= last; first++, last--) { if (first == last) { cout << "中間數據為:" << ivec[last] << endl; } else cout << ivec[first] + ivec[last] << endl; } return 0; }
運行結果:
可以使用vector可以創造動態數組(如上),也可以使用
int n;
cin>>n;
int *array=new int [n];