|
: 1+(1+2)+(1+2+3)+ ............ +(1+2+3+4+......+19+20) 의 합을 구하시오..
STL을 써서, for 루프를 쓰지 않고 구현하는 방법입니다.
// Illustrating the generic partial_sum algorithm
#include <iostream>
#pragma hdrstop
#include <algorithm>
#include <numeric>
#include <vector>
#include <iterator>
using namespace std;
int main()
{
cout << "Illustrating the generic iota and partial_sum algorithm."
<< endl;
const int N = 20;
vector<int> x1(N), x2(N);
// ANSI C++ 표준에는 없습니다: 빌더 5이하에서는 STLport를 설치하세요.
iota(x1.begin(), x1.end(), 1);
copy(x1.begin(), x1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
// Compute the partial sums of 1, 2, 3, ..., N
// putting the result in x2:
partial_sum(x1.begin(), x1.end(), x2.begin());
copy(x2.begin(), x2.end(), ostream_iterator<int>(cout, " "));
cout << endl;
return 0;
}
|