N!
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 75303 Accepted Submission(s): 21981
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
Author
求n的阶乘嘛! 基础题,依然WA,也是够了!
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<stdlib.h>
#include<queue>
typedef long long ll;
using namespace std;
#define INF 0x3f3f3f3f
#define N 1100000
int a[N];
int main()
{
int i,j,n,count,t,k;
while(scanf("%d",&n)!=EOF)
{
a[0]=1;
count=1;
for(i=1;i<=n;i++)
{
k=0;
for(j=0;j<count;j++)
{
t=a[j]*i+k;
a[j]=t%10;
k=t/10;
}
while(k)
{
a[count++]=k%10;
k=k/10;
}
}
for(i=count-1;i>=0;i--)
{
printf("%d",a[i]);
}
printf("\n");
}
return 0;
}
10000的阶乘也就35660位,要是担心TLE,或者超限的话,也可以按10000进制来写:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<stdlib.h>
#include<queue>
typedef long long ll;
using namespace std;
#define INF 0x3f3f3f3f
#define N 1100000
int a[N];
int main()
{
int i,j,n,count,t,k;
while(scanf("%d",&n)!=EOF)
{
a[0]=1;
count=1;
for(i=1;i<=n;i++)
{
k=0;
for(j=0;j<count;j++)
{
t=a[j]*i+k;
a[j]=t%10000;
k=t/10000;
}
while(k)
{
a[count++]=k%10000; ///这里以10000为进位单位
k=k/10000;
}
}
printf("%d",a[count-1]); ///注意一下,第一个数不是“%04d”;
for(i=count-2;i>=0;i--)
{
printf("%04d",a[i]); ///其他的按四位的输出,不足补零
}
printf("\n");
}
return 0;
}
转载请注明原文地址: https://ju.6miu.com/read-1311872.html