我試圖開發SPOJ階乘問題數11.下面的代碼是我的代碼SPOJ階乘問題
import java.math.*;
import java.io.*;
public class Problem11 {
/**
* Count the number of zeroes at the end of
* the factorial value of a number.
*/
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int numOfInputs=0;
numOfInputs=Integer.parseInt(br.readLine());
BigInteger nextNum[]=new BigInteger[numOfInputs];
BigInteger factValue[]=new BigInteger[numOfInputs];
//Get all the numbers to be computed
for(int count=0;count<numOfInputs;count++)
{
nextNum[count]=new BigInteger(br.readLine());
}
//Obtain the factorial value for each number
for(int count=0;count<numOfInputs;count++)
{
factValue[count]=getFact(nextNum[count]);
}
//Obtain the number of trailing zeroes
for(int count=0;count<numOfInputs;count++)
{
//System.out.println(factValue[count]);
System.out.println(getZeroes(factValue[count]));
}
}
public static String getZeroes(BigInteger num)
{
int numOfZeroes=0;
while(num.remainder(BigInteger.TEN).equals(BigInteger.ZERO))
{
num=num.divide(BigInteger.TEN);
numOfZeroes++;
}
return String.valueOf(numOfZeroes);
}
public static BigInteger getFact(BigInteger num)
{
BigInteger factorial=BigInteger.ONE;
if(num.equals(0))
{
return (BigInteger.valueOf(1));
}
else
{
int count=1;
while((BigInteger.valueOf(count).compareTo(num))<=0)
{
factorial=factorial.multiply(BigInteger.valueOf(count));
count++;
}
}
return factorial;
}
}
代碼工作罰款數達5位數字以小的延遲,並在過去的數 8735373
它花費了太多時間,如果我提交我的解決方案,法官顯示編譯錯誤..我無法弄清楚錯誤是什麼。請看看我的代碼,並幫助我找出問題所在。
這不是那裏最快的,但它是一個開始。 – trashgod