import java.util.Scanner;
public class Factorial
{
public static void main(String[] args)
{
    int[] result=new int[10000000];
    int digit=1;
    for(int i=0;i<result.length;i++)
    {
    result[i]=0;
    }
    Scanner sc = new Scanner( System.in );
    System.out.print("pleease input the number you want compute:");
 
int n=Integer.parseInt(sc.next());
result[0]=1;


for(int i=1;i<n+1;i++)
{
for(int j=0;j<digit;j++)
{
result[j]=result[j]*i;
}
  
for(int j=0;j<digit;j++)
 {
if(result[digit]>10)
{ digit++;
}
result[j+1]+=result[j]/10;
result[j]=result[j]%10;
}
}







System.out.print(n+"!=");
  for(int k=digit-1;k>=0;k--)
{System.out.print(result[k]);}
        System.out.println("");
 
}
}
运行时结果不正确:4!=4,5!=0,请帮我找一下问题,多谢了

解决方案 »

  1.   

    为啥不用BigInteger和BigDecimal?
      

  2.   


    import java.util.Scanner;public class Factorial { public long JieChen(int num) {
    long result = 0L;

    for (int i = 0; i <= num; i++) {
    if (num == 1) {
    result = 1;
    } else {
    result = i * JieChen(num - 1);
    }
    }
    return result;
    } public static void main(String[] args) {

    Factorial factorial = new Factorial();

    System.out.print("please input the number you want compute :");

    Scanner scanner=new Scanner(System.in);
    int number=scanner.nextInt();

    long result = factorial.JieChen(number);

    System.out.println(number+"! = "+result);
    }
    }
    以上是用递归算法实现的阶层,供LZ参考,至于大数字的阶层的实现,还是LZ自己动手做吧
      

  3.   

    大数要用BigInteger和BigDecimal
      

  4.   

    挑个小毛病 LZ 代码中
    int[] result=new int[10000000];
    int digit=1;
    for(int i=0;i<result.length;i++)
    {
    result[i]=0;
    }
    是多余的,因为int[] result=new int[10000000];就初始化了。
    还有没必要申请那么大的数组吧,递推的话记录当前一个就可以了,没必要把前边用过的都记录下来的。
    另附上 递归的代码
    private int GetFactorial(int i)
            {
                if (i < 2) return 1;
                return i * GetFactorial(i - 1);
            }
      

  5.   

    搂主对边界的判断有失误,按照搂主的算法,result[]中的最高位的元素值最大不超过99,实际是可能存储了任意〉0的int。下面的修改后打代码,希望有帮助。
    import java.util.Scanner;public class Factorial {
    public static void main(String[] args) {
    int[] result = new int[10000000];
    int digit = 1;
    for (int i = 0; i < result.length; i++) {
    result[i] = 0;
    }
    Scanner sc = new Scanner(System.in);
    System.out.print("pleease input the number you want compute:"); int n = Integer.parseInt(sc.next());
    result[0] = 1; for (int i = 1; i < n + 1; i++) {
    for (int j = 0; j < digit; j++) {
    result[j] = result[j] * i;
    }
    for (int j = 0; j < digit; j++) {
    /*
     * the operator to boundary is incorrect,
     * when j= digit-1;the value of result[j] may be > 99;
     * So such value's digits should be more than 2.
     */
    // if (result[digit] > 10) {
    // digit++;
    // }
    if(j<digit-1){
    result[j + 1] += result[j] / 10;
    result[j] = result[j] % 10;
    }else{
    /*
     * use this way 
     */
    String tempStr = new StringBuffer(result[j]+"").reverse().toString();
    int tempStrLength = tempStr.length();
    digit = digit+tempStrLength-1;
    for(int z=0;z<tempStrLength;z++){
    result[j+z] = tempStr.charAt(z)-48;
    }
    break;
    }

    }
    } System.out.print(n + "!=");
    for (int k = digit - 1; k >= 0; k--) {
    System.out.print(result[k]);
    }
    System.out.println("");
    }
    }