代码不长,我让循环中所有要做的事全做完了,不另外开循环,这样省了很多时间
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5+10;
int a[N],b[N];
int top = 1,siz_a=1;
signed main()
{
int n;cin >>n;
b[1] = 1;
for(int i = 1 ; i <= n ; i++) {
for(int j = 1 ; j <= top ; j++){
b[j]*=i;
}
if(siz_a<top) siz_a = top;
for(int j = 1 ; j <= siz_a ; j++){
if(b[j]>9&&j<=top){
b[j+1] +=b[j]/10;
b[j]%=10;
if(j+1>top) top++,siz_a++;
}
if(j<=top) a[j]+=b[j];
if(a[j]>9){
a[j+1]+=a[j]/10;
a[j]%=10;
if(j+1>siz_a) siz_a++;
}
}
}
for(int i = siz_a ; i >= 1 ; i--) cout << a[i];
cout << endl;
return 0;
}