-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.20.cpp
37 lines (32 loc) · 909 Bytes
/
3.20.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Read a set of intergers into a vector
// Print the sum of each pair of adjacent
// elements and the sum of the first and
// last elements and the sum of the second
// and second-to-last, and so on
// Xiaoyan Wang 11/18/2015
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> ivec;
int temp;
while(cin >> temp)
ivec.push_back(temp);
cout << "Sums of the number pairs:" << endl;
for(decltype(ivec.size()) i = 0; i < ivec.size(); i+=2) {
if(i == ivec.size()-1)
cout << ivec[i] << endl;
else
cout << ivec[i] + ivec[i+1] << endl;
}
cout << "Sums of the first and the last elements:" << endl;
for(decltype(ivec.size()) i = 0; i < ivec.size()/2+1; ++i) {
if(i == ivec.size()/2 + 1)
if(!(ivec.size() % 2))
break;
else
cout << ivec[i] << endl;
cout << ivec[i] + ivec[ivec.size()-1-i] << endl;
}
return 0;
}