简体   繁体   中英

I am getting fine results in ideone but SPOJ is not accepting my solution. The link is given below

http://www.spoj.com/problems/FCTRL2/

It works fine when I tested with some of the test cases. But SPOJ says wrong answer. What can I do so that they accept the code?

import java.util.*;

class testw
{   
public static void main(String args[])
{
       Scanner obj = new Scanner(System.in);
       int t=obj.nextInt();
       int count = t;
       int arr[]= new int[t];
       int res[] =new int[t];
       int i=0;
       while(t>0)
       {
           arr[i]=obj.nextInt();
           res[i]=func(arr[i]);
           i++;
           t--;
       }

       for(int j=0; j<count; j++) 
       {
           System.out.println(res[j]);
        }
    }

    public static int func(int a)
    {
        int f=1;
        for(int i=1;i<=a;i++)
        {
            f= f*i;
        }
        return f;
    }}

Your code is unable to handle really big numbers. You can see that problem description constraints state that 1 ≤ n ≤ 100 . You should be aware that 100! has 159 digits while int type that you have used is bound by a couple of billions (ie about 10 digits).

Your code works fine on Ideone because you try it with small numbers. You should expect that during correctness verification Spoj use more advanced test cases (including bigger numbers).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM