[英]Program to find factorials for given test cases
Input输入
An integer t, 1<=t<=100, denoting the number of test cases, followed by t lines, each containing a single integer n, 1<=n<=100.一个整数 t,1<=t<=100,表示测试用例的数量,后跟 t 行,每行包含一个整数 n,1<=n<=100。
Output输出
For each integer n given at input, display a line with the value of n!对于输入处给出的每个整数 n,显示一行,其值为 n!
I have typed the code below and it doesn't get accepted on codechef.我已经输入了下面的代码,但它没有被 codechef 接受。
I don't know why.我不知道为什么。
#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.我已经输入了下面的代码,但它没有被 codechef 接受。
I don't know why.我不知道为什么。
OP's code fails as double
lacks precision for this task. OP 的代码失败,因为double
缺乏此任务的精度。 Given "containing a single integer n, 1<=n<=100."鉴于“包含单个整数 n,1<=n<=100。” and fact(100)
, the printed result needs far more precision than afforded by double
.和fact(100)
,打印结果需要比double
提供的精度高得多的精度。
100! 100! is exactly正是
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
double
(precision) and uint64_t
(range) are both insufficient for this task. double
(精度)和uint64_t
(范围)都不足以完成此任务。 Need new approach.需要新方法。 Something that extends exact integer multiplication.扩展精确整数乘法的东西。
Perhaps create a function that multiplies a string ?也许创建一个乘以字符串的函数? mult_str(char *s, int n)
? mult_str(char *s, int n)
? "1"
* 2 --> "2"
. "1"
* 2 --> "2"
。
Do-able in about 30 lines of C code without special libraries.无需特殊库即可在大约 30 行 C 代码中完成。
This seemed like an interesting short program so I wrote it up and submitted it to CodeChef.这似乎是一个有趣的短程序,所以我写了它并提交给 CodeChef。 It was accepted on the first attempt -- not bad for BOTE (Back of the Envelope) code.它在第一次尝试时就被接受了——对于 BOTE(信封背面)代码来说还不错。
/* FCTRL2.c
CodeChef Problem Code: FCTRL2 (Small Factorials)
*/
#include <stdio.h>
#define LEN 158
int nextFactorial (const int n, char fact[], int limit)
{
int carry = 0;
for ( int index = LEN - 1; index >= limit; --index ) {
int product = fact[index] * n + carry;
fact[index] = product % 10;
carry = 0;
if ( product > 9 ) {
carry = product / 10;
if ( index == limit )
--limit;
}
}
return limit;
}
void displayFactorial (const char fact[], const int limit)
{
for ( int index = limit; index < LEN; ++index )
printf ("%c", fact[index] + '0');
printf ("\n");
}
int main (void)
{
int count;
scanf ("%i", &count);
int n[count];
for ( int i = 0; i < count; ++i )
scanf ("%i", &n[i]);
for ( int i = 0; i < count; ++i ) {
char fact[LEN] = { [LEN - 1] = 1 };
int limit = LEN - 1;
for ( int j = 2; j <= n[i]; ++j )
limit = nextFactorial (j, fact, limit);
displayFactorial (fact, limit);
}
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.