的阶乘它是在一个网站的测试,这里是代码是计算大数
#include <stdio.h>
void Print_Factorial (const int N);
int main()
{
int N;
scanf("%d",&N);
Print_Factorial(N--);
return 0;
}
/* your code will be put in here*/
#include <math.h>
int getFactLength(int N){
double length = 0;
while(N){
length += log10(N--);
}
return (int)length+1;
}
void printFact(int fact[], int length){
while(length--){
printf("%d",*fact++);
}
}
void initialNums(int nums[], int length, int num){
while(length--){
*nums++ = num;
}
}
void Print_Factorial(const int N){
if(N < 0){
printf("Invalid input");
return ;
}
int NT = N;
if(NT>=0 && NT<15){
int fact = 1;
while(NT){
fact *= NT--;
}
printf("%d",fact);
return ;
}
int length = getFactLength(N);
int fact[length];
initialNums(fact, length, 0);
fact[length-1] = 1;
int lastNoneZeroIndex = length-1;
while(NT > 1){
int lengthT = length;
int carry = 0;
while(lengthT-- > lastNoneZeroIndex){
int result = NT*fact[lengthT] + carry;
fact[lengthT] = result % 10;
carry = result/10;
}
while(carry){
fact[--lastNoneZeroIndex] = carry % 10;
carry /= 10;
}
NT--;
}
printFact(fact, length);
}
我使用0
到20
值进行测试,所有的人都是正确的程序。但是当我在该网站上提交它时,测试用例总是不通过。我不知道那是什么情况。但是,有5个案例,所有测试案例都在0到1000之间,其中两个不超过15个,其中一个是否定的,其中一个使用最多时间通过,所以我认为未通过的案例是一个小于1000的数字。这就是我所知道的,我无法想象1000个通过了,但小于1000的数字没有通过。我不知道我可爱的代码有什么问题。我希望你能看我的代码,并找到一些错误。
尝试测试你的大投入方案了。例如,您是否检查过,如果输入为100,您的程序将返回93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000? – Ilya
你的程序实际上计算1000!正确。我相信你有一个格式问题。 –
在阶乘后不打印新行。也许自动chacker期待这一点。 (一个新行也会刷新输出。) –