1001–求和n+(n-1)+……+1
————AC代码————
//In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n.
#include <iostream>
using namespace std;
int SUM(int n) {
if (n <= 1)
return n;
else
return (n + SUM(n - 1));
}
int main() {
int n = 0, sum = 0;
while (cin >> n) {
sum = SUM(n);
cout << sum << endl << endl;
sum = 0;
}
return 0;
}