#include <iostream>

using namespace std;
//2.用递归的方法求1+2+...+n。

int test(int n){
    if(n==1) return 1;
    if(n==0) return 0;
    if(n<0) return 0;
    return n+test(n-1);
}

int main()
{
    int n;
    cout<<"请输入一个数:"<<endl;
    cin>>n;
    cout << test(n) << endl;
    return 0;
}

更多推荐