输入
一个整数t,1<=t<=100,表示测试用例的数量,后面是t行,每行包含一个整数n,1<=n<=100。
输出
对于输入时给定的每个整数 n,显示带有 n 值的行!
我已经输入了下面的代码,但 codechef 没有接受它。
我不知道为什么。
#include<stdio.h>
double fact(int n);
void disp(double t);
int main() {
int a, i, c[100];
scanf("%d", &a);
for (i = 0; i < a; i++) {
scanf("%d", &c[i]);
}
for (i = 0; i < a; i++) {
disp(fact(c[i]));
}
return 0;
}
double fact(int n) {
double f;
if (n == 1 || n == 0)
f = (double) 1;
else
f = (double) n * fact(n - 1);
return f;
}
void disp(double t) {
printf("%f\n", t);
}
最佳答案
I have typed the code below and it doesn't get accepted on codechef.
I don't know why.
OP 的代码失败,因为 double
缺乏此任务的精度。给定“包含单个整数 n,1<=n<=100”。和 fact(100)
,打印结果需要的精度远高于 double
所提供的精度。
100!正是
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
double
(精度)和uint64_t
(范围)都不足以完成此任务。需要新的方法。扩展精确整数乘法的东西。
也许创建一个与字符串相乘的函数? mult_str(char *s, int n)
? “1”
* 2 --> “2”
。
无需特殊库,只需大约 30 行 C 代码即可完成。
关于c - 为给定测试用例查找阶乘的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52084912/