xinggancaixukun @ 2023-07-25 10:23:38
#include <bits/stdc++.h>
using namespace std;
int a[50];
int main(){
int n,sum;
cin>>n;
for(int i=1;i<=n;i++){
a[i]=a[i-1]*i;
}
for(int i=1;i<=n;i++){
sum+=a[i];
}
cout<<sum;
return 0;
}
by 2011FYCCCTA @ 2023-07-25 10:39:43
……看楼上
by xinggancaixukun @ 2023-07-25 10:40:57
#include <bits/stdc++.h>
using namespace std;
int a[50];
int main(){
int n,sum;
cin>>n;
a[0]=1;
for(int i=1;i<=n;i++){
a[i]=a[i-1]*i;
}
for(int i=1;i<=n;i++){
sum+=a[i];
}
cout<<sum;
return 0;
}
by xinggancaixukun @ 2023-07-25 10:41:24
输出 10 事怎么会事?
by LiJoQiao @ 2023-07-25 10:41:37
@xinggancaixukun 应给 a[0]
赋值 int i=2
。
否则会在循环中覆盖 a[1]
为
by xinggancaixukun @ 2023-07-25 10:42:02
@LiJoQiao 谢谢
by LiJoQiao @ 2023-07-25 10:42:25
@xinggancaixukun sum
需要初始化。
by xinggancaixukun @ 2023-07-25 10:42:42
哦,sum 忘记给以个 0 的初始值了
by xinggancaixukun @ 2023-07-25 10:43:04
@LiJoQiao 谢谢%%%
by xinggancaixukun @ 2023-07-25 10:44:05
开了 long long 怎么还是过不了!
by Ted_LightningTechG_ @ 2023-07-25 10:44:10
@xinggancaixukun 定义全局变量就可以少操这个心