N!
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 71035 Accepted Submission(s): 20373
Problem Description
Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N!
Input
One N in one line, process to the end of file.
Output
For each N, output N! in one line.
Sample Input
1 2 3
Sample Output
1 2 6
#include<cstring>
#include<cstdio>
int num[50000];
int main(){
int n;
while( scanf("%d",&n) != EOF){
memset(num, 0, sizeof(num));
num[0] = 1, num[1] = 1;
for(int i = 2; i <= n; ++i){
int len = num[0];
for(int j = 1;j <= len; ++j) num[j]*=i;
for(int j = 1; j <= num[0]; ++j){
if( num[j] > 9) num[j+1] += num[j]/10, num[j] %= 10;
if( num[ num[0] +1]) num[0] += 1;
}
}
for(int i = num[0];i >= 1; --i) putchar(num[i]+'0');
puts("");
}
}
自己的代码>_< WA
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
typedef int* bignptr;
int basic[100000];
bignptr a=basic;
int main()
{
int tmp,i,j,bignlen;
while(scanf("%d",&tmp)==1)
{
switch(tmp)
{
case 0:
printf("0\n");
continue;
case 1:
printf("1\n");
continue;
case 2:
printf("2\n");
continue;
case 3:
printf("6\n");
continue;
case 4:
printf("24\n");
continue;
case 5:
printf("120\n");
continue;
case 6:
printf("720\n");
continue;
case 7:
printf("5040\n");
continue;
case 8:
printf("40320\n");
continue;
case 9:
printf("362880\n");
continue;
case 10:
printf("3628800\n");
continue;
case 11:
printf("39916800\n");
continue;
case 12:
printf("479001600\n");
continue;
case 13:
printf("6227020800\n");
continue;
case 14:
printf("87178291200\n");
continue;
case 15:
printf("1307674368000\n");
continue;
case 16:
printf("20922789888000\n");
continue;
case 17:
printf("355687428096000\n");
continue;
}
memset(a,0,sizeof(int)*100000);
a[3]=6;
a[4]=9;
a[6]=8;
a[7]=2;
a[8]=4;
a[9]=7;
a[10]=8;
a[11]=6;
a[12]=5;
a[13]=5;
a[14]=3;
bignlen=15;
for(i=18; i<=tmp; i++)
{
for(j=0; j<bignlen; j++)
{
a[j]=a[j]*i;
}
for(j=0; j<bignlen; j++)
{
if(a[j]>10)
{
a[j+1]+=a[j]/10;
a[j]=a[j]%10;
}
}
for(j=bignlen; a[j]>0; j++)
{
if(a[j]>10)
{
a[j+1]+=a[j]/10;
a[j]=a[j]%10;
}
}
bignlen=j;
}
for(i=bignlen-1; i>-1; i--)
{
printf("%d",a[i]);
}
printf("\n");
}
return 0;
}
So sad...