codechef:在smallfactorial错误答案错误 [英] codechef :wrong answer error in smallfactorial

查看:158
本文介绍了codechef:在smallfactorial错误答案错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>
int fact(int k)
{
int j,f=1;
for(j=1;j<=k;j++)
f*=j;
return f;
}
int main()
{
int t,i,n[100],s[100],j;
scanf("%d",&t);
for(i=0;i<t;i++)
{
scanf("%d",&n[i]);
}
for(j=0;j<t;j++)
{
s[j]=fact(n[j]);
printf("%d \n",s[j]);
}
return 0;
}

您被要求计算出一些小的正整数的阶乘。
输入

You are asked to calculate factorials of some small positive integers. Input

这是整数t,1所述; = T&下; = 100,表示测试用例的数量,接着为T行,每一个包含单个整数n,1&下; =正&下; = 100。
输出

An integer t, 1<=t<=100, denoting the number of testcases, followed by t lines, each containing a single integer n, 1<=n<=100. Output

有关各整数n在输入定,显示与n的值的线!
示例

For each integer n given at input, display a line with the value of n! Example

样品输入:
4
1
2

3
输出示例:
1
2
120
6

Sample input: 4 1 2 5 3 Sample output: 1 2 120 6

推荐答案

您code会给出正确的结果对于给定的测试案例,但这并不证明你的code ++工程。这是错误的,因为整数溢出。尝试通过程序来计算 100 <!/ code>,你会看到什么问题。

Your code will give correct results for the given test cases but that doesn't prove that your code works. It is wrong is because of integer overflow. Try to calculate 100! by your program and you'll see what's the problem.

我的回答缺乏细节。我会更新这个补充细节的问题的答案,因为它现在站立。

My answer lacked details. I'll update this to add details for an answer to the question as it stands now.

C具有通过可存储在一个变量的最大和最小尺寸的限制。换做任意precision算法通常是建议使用BIGNUM库PHIFounder曾建议。

C has limitations over the the maximum and minimum size that can be stored in a variable. For doing arbitrary precision arithmetic it is usually advisable to use a bignum library as PHIFounder has suggested.

在present壳体然而,使用外部库的是不可能的。在这种情况下,阵列可用于存储整数超过可能的整数的最大值。 OP已经找到了这种可能性,并用它。她实施,但是,可以使用很多的优化。

In the present case however, the use of external libraries is not possible. In this case arrays can be used to store integers exceeding the maximum value of the integers possible. OP has already found this possibility and used it. Her implementation, however, can use many optimizations.

最初的使用大阵列那样的可被减少。代替使用100个变量的单一变量的阵列可以被用来存储测试用例。使用大阵和阅读测试案例能给的也只有如果您正在使用缓冲区从标准输入读入优化否则将不会比打电话更好的 scanf函数通过添加读取测试用例的 scanf函数循环对于打算在单个的测试案例。

Initially the use of large arrays like that can be reduced. Instead of using an array of 100 variables a single variable can be used to store the test cases. The use of large array and reading in test cases can give optimization only if you are using buffers to read in from stdin otherwise it won't be any better than calling scanf for reading the test cases by adding a scanf in the for loop for going over individual test cases.

这是你的选择,要么利用缓冲来获得速度的提升和制作一个整数,而不是100整数数组中。在这两种情况下,将有超过联系到当前解决方案的改进,在codechef,由OP。对于缓冲您可以参考这个问题 。如果您看到$ C $的时序结果cchef缓冲的结果可能是不可见的,因为操作都必须在逻辑的其余部分数量很大。

It's your choice to either use buffering to get speed improvement or making a single integer instead of an array of 100 integers. In both the cases there will be improvements over the current solution linked to, on codechef, by the OP. For buffering you can refer to this question. If you see the timing results on codechef the result of buffering might not be visible because the number of operations in the rest of the logic is high.

关于使用数组[200]现在第二件事。在codechef的博客教程使用200个元素的数组用于演示的逻辑。这是一种幼稚的方法,因为它本身所指出的教程。在每个阵列位置存储一个数字是一个巨大的浪费内存。这种方法也导致了多操作导致一个较慢的溶液。一个整数,至少可以存储5位(-32768至32767)和一般存储更多。您可以将中间结果存储在得到long long int 作为你的温度并使用所有5位数字。这简化本身将导致只使用的改编[40] 而不是的改编[200] 。在code将需要一些额外的变化照顾弘扬,并会成为一个稍微复杂一些,但速度和内存改进将是可见的。

Now second thing about the use of array[200]. The blog tutorial on codechef uses an array of 200 elements for demonstrating the logic. It is a naive approach as the tutorial itself points out. Storing a single digit at each array location is a huge waste of memory. That approach also leads to much more operations leading to a slower solution. An integer can at least store 5 digits (-32768 to 32767) and can generally store more. You can store the intermediate results in a long long int used as your temp and use all 5 digits. That simplification itself would lead to the use of only arr[40] instead of arr[200]. The code would need some additional changes to take care of forward carry and would become a little more complex but both speed and memory improvements would be visible.

您可以参考<一个href=\"http://www.$c$cchef.com/status/FCTRL2,anshbansal?sort_by=Time&sorting_order=asc&language=All&status=15\"相对=nofollow>这个为看到我的解决方案,或者你可以看到的这个具体的解决方案。我能只需要使用到 26 元素,并有可能采取进一步下降。

You can refer to this for seeing my solutions or you can see this specific solution. I was able to take the use down to 26 elements only and it might be possible to take it further down.

我会建议你要忍受在 codereview 与code让你的code审查。有许多待审查最好有更多的问题。

I'll suggest you to put up your code on codereview for getting your code reviewed. There are many more issues that would be best reviewed there.

这篇关于codechef:在smallfactorial错误答案错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆