C++ 高效计算 S = 1 + (1+2) + (1+2+3) + ... + (1+2+3+...+n)
#include
int main() { int n; cin >> n;
int s = 0;
int sum = 0;
for(int i=1; i<=n; i++) {
sum += i;
s += sum;
}
cout << s << endl;
return 0;
}
原文地址: https://www.cveoy.top/t/topic/qjhK 著作权归作者所有。请勿转载和采集!