
#include<bits/stdc++.h>
using namespace std;
int fun(int n){
if(1==n) return 1;
else return fun(n-1)+n;
}
int main(){
int x,s=0;
cin>>x;
for (int i = 1; i <= x; i++) {
s= s+fun(i);
}
cout<<s<<endl;
}
本文介绍了一个使用C++编写的递归函数,该函数用于计算1到输入整数x的阶乘之和。在main函数中,通过循环调用fun函数并累加结果。

#include<bits/stdc++.h>
using namespace std;
int fun(int n){
if(1==n) return 1;
else return fun(n-1)+n;
}
int main(){
int x,s=0;
cin>>x;
for (int i = 1; i <= x; i++) {
s= s+fun(i);
}
cout<<s<<endl;
}
1020
252
221

被折叠的 条评论
为什么被折叠?