64,663
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
using namespace std;
long int fun(long int);
int main()
{
int number;
int n[1000];
int i;
long int S;
cin>>number;
for(i=0;i<number;i++)
{
cin>>n[i];
}
for(i=0;i<number;i++)
{
S=0;
long int j;
for(j=1;j<n[i];j++)
{
S+=j*fun(j);
}
cout<<S%n[i]<<endl;
}
return 0;
}
long int fun(long int k)
{
long int f;
if(k==0||k==1)
{
f=1;
}
else
f=fun(k-1)*k;
return f;
}
用迭代,不要用递归
#include<stdio.h>
#define N 5000//modify it to hold larger number
//to avoid stack overflow, define it as global varible
int f[N];//do not need to be assigned as 0 because it has already been assigned 0 by the system
int main()
{
int n,i,j,s,up;
scanf("%d",&n);
for(i=2,f[0]=1;i<=n;i++)
{
for(j=up=0;j<N;j++)
{
s=f[j]*i+up;
f[j]=s%10;
up=s/10;
}
}
for(i=N-1;f[i]==0;i--);
for(;i>=0;i--)
printf("%d",f[i]);
printf("\n");
return 0;
}
引用#include<stdio.h> #define N 5000//modify it to hold larger number //to avoid stack overflow, define it as global varible int f[N];//do not need to be assigned as 0 because it has already been assigned 0 by the system int main() { int n,i,j,s,up; scanf("%d",&n); for(i=2,f[0]=1;i<=n;i++) { for(j=up=0;j<N;j++) { s=f[j]*i+up; f[j]=s%10; up=s/10; } } for(i=N-1;f[i]==0;i--); for(;i>=0;i--) printf("%d",f[i]); printf("\n"); return 0; }