- 論壇徽章:
- 0
|
本帖最后由 hniu 于 2013-07-20 21:46 編輯
小侄女讀初中,問(wèn)我一道奧賽試題:
從鍵盤(pán)輸入30個(gè)數(shù),每5個(gè)求和,結(jié)果保存在一個(gè)數(shù)組中,輸出。
斗膽寫(xiě)了一段代碼:
- #include <algorithm>
- #include <numeric>
- #include <iostream>
- #include <array>
- int main()
- {
- using namespace std;
- array<int, 30> myarray;
- array<int, 6> myret;
- for(auto& i : myarray)
- cin >> i;
- for_each(myret.begin(), myret.end(), [&myarray](int& s){
- static int i=0;
- auto itr=myarray.begin();
- s=accumulate(itr+i, itr+i+5, 0);
- i+=5;
- });
-
- for(auto& i : myret)
- cout << i << " ";
- cout << endl;
- return 0;
- }
-
復(fù)制代碼 |
|