1+2+3+4+....+10 用C++编写
求帮忙 谢谢了 好多年没用了 忘记了程序代码:
sum = 0; for(i=1;i<=10;i++) sum += i;
#include <iostream> #include <vector> #include <algorithm> using std::cout; using std::endl; using std::vector; using std::for_each; const int N = 10; int main(void) { vector<int> a(N); int i = 1; int sum = 0; for_each (a.begin(), a.end(), [&i] (int& a) {a = i++;}); for_each (a.begin(), a.end(), [&sum] (int& a) {sum += a;}); cout << sum << endl; return 0; }