Java SPOJ阶乘问题

Java SPOJ阶乘问题,java,Java,我正在尝试为SPOJ阶乘问题11开发代码。下面是我的代码 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 { Buffer

我正在尝试为SPOJ阶乘问题11开发代码。下面是我的代码

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;

}

}
import java.math.*;
导入java.io.*;
公共课问题11{
/**
*在计算结束时计算零的数量
*一个数的阶乘值。
*/
公共静态void main(字符串[]args)引发IOException
{
BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in));
int numOfInputs=0;
numOfInputs=Integer.parseInt(br.readLine());
BigInteger nextNum[]=新的BigInteger[numOfInputs];
BigInteger factValue[]=新的BigInteger[numOfInputs];
//获取要计算的所有数字
因为(int count=0;count您的方法(天真:计算真实的阶乘值,然后手动计算零)无论如何都不会通过。看看极端情况(即阶乘的上限,我甚至不认为给定的内存限制足以计算它).从不同的角度看待问题,思考真正的问题是什么,这就是解决问题的艺术;)


提示:什么可以产生更多的0并将其添加到一个数字的末尾,特别是通过乘法?

产生错误的原因是它应该是
public class Main


此外,您的代码将得到TLE,您必须注意,暴力永远不会对SPOJ起作用。解决这一问题的方法是查看一个有趣的模式,其幂为5,最后的数字为零。

您可以使用此方法进行查找

import java.util.*;
公共班机{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
int count=in.nextInt();
for(int i=0;i对于(int d=5;d,这里有一个C中的解。我们不需要计算这个问题的精确阶乘

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX 1000000
int xpn(int x, int n){
int prod=1;
while(n){
prod*=x;
n--;
}
return prod;
}
int trail(int x){
int numZero=0;
int i=1;
int k;
for(;x/xpn(5,i);i++){
numZero+=(x/xpn(5,i));
}
return numZero;
}
int main(int argc, char **argv){
#if 1
int n;
int num[MAX];
int zero[MAX];
scanf("%d",&n);
int count=n;
int i=0;
while(count){
scanf("%d",&num[i]);
zero[i]=trail(num[i]);
printf("%d\n",zero[i]);
i++;
count--;
}
#endif
return 0;
}
#包括
#包括
#包括
#定义最大1000000
intxpn(intx,intn){
int prod=1;
while(n){
prod*=x;
n--;
}
返回产品;
}
int trail(int x){
int numZero=0;
int i=1;
int k;
对于(;x/xpn(5,i);i++){
numZero+=(x/xpn(5,i));
}
返回numZero;
}
int main(int argc,字符**argv){
#如果1
int n;
int num[MAX];
int零[MAX];
scanf(“%d”和“&n”);
整数计数=n;
int i=0;
同时(计数){
scanf(“%d”、&num[i]);
零[i]=trail(num[i]);
printf(“%d\n”,零[i]);
i++;
计数--;
}
#恩迪夫
返回0;
}
这个解决方案绝对有效。 让我解释一下。 当你提到定量能力时,有一个简短的公式可以计算任何阶乘数的尾随零的数量。直接将该数字除以5,然后开始加商,再除以商5,再加上一次,直到该值开始给出常量商。参考下面的示例。如果你不理解,请参考o任何来源的定量能力

e、 g


感谢您的直接帮助,您能否确认我获取阶乘值的方法是否正确,以便我考虑一下获取值中的零数?您不需要获取阶乘值:)正如耶稣所说,不。你不需要它,你只需要得到导致0的因子。另一个提示:解决方案的复杂性是O(1):)哦!我感到困惑和太激动了,我的代码的哪一部分需要更改。非常感谢你的回答。你能帮我解决这类问题吗?蛮力=坏。让我给你一点提示。乘以什么数字加1零?那么n!包含的零(在最后)因为n中有这些数字的因素。现在我得到一个运行时错误NZEC。我已经将类名更改为Main。至于你的方法,如果我将一个数字乘以10,我将得到一个零。例如,如果
100!
值是
9332621554439415268169923885626670049071596826431621468595938959638952175993229915649463976156182862536979208272237582511852109168640000000000000000000000000000000000000000000000000000000
有24个零。请告诉我逼近10不是唯一的数字,如果它以2结尾,我乘以5会怎么样?NZEC也意味着你的程序崩溃或抛出异常。这不是最快的一个,但这是一个开始。我看到你一直在使用java的BigInteger类。对于这个问题,您不需要计算阶乘。这是一个低效的解决方案。请查看教程和我的C实现以供参考。我希望您届时能够用Java编写它。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX 1000000
int xpn(int x, int n){
int prod=1;
while(n){
prod*=x;
n--;
}
return prod;
}
int trail(int x){
int numZero=0;
int i=1;
int k;
for(;x/xpn(5,i);i++){
numZero+=(x/xpn(5,i));
}
return numZero;
}
int main(int argc, char **argv){
#if 1
int n;
int num[MAX];
int zero[MAX];
scanf("%d",&n);
int count=n;
int i=0;
while(count){
scanf("%d",&num[i]);
zero[i]=trail(num[i]);
printf("%d\n",zero[i]);
i++;
count--;
}
#endif
return 0;
}
import java.util.*;
import java.lang.*;

class Main
{
    public static void main (String[] args) throws java.lang.Exception
    {
        Scanner s = new Scanner(System.in);
        int t = s.nextInt();
        while (t-->0){
            int n = s.nextInt();
            int count = 0;
            while(n>0){
                count +=n/5;
                n/=5;
            }
            System.out.println(count);
        }

    }
}
1. 60!  60/5 = 12, 12/5 = 2, add all the quotient i.e equals to 14.
2. 500! 500/5 = 100, 100/5 = 20, 20/5 = 4, ans = 124.