题目要求1!+2!+3!+4!+...+1000!
一下是我的代码:哪里有问题啊!
//这个程序那里错了
import java.util.*;
import java.math.*;
public class Test5 {
static BigInteger fun1(BigInteger n)
{
BigInteger i;
BigInteger result=1;
for(i=1;i.subtract(n)!=0;i.add(1))
{
result=result.multiply(i);
}
return result;
}
static BigInteger fun(BigInteger n)
{
BigInteger result =0;
BigInteger i=BigInteger.valueOf(1);
for(;i.subtract(n)!=0;i.add(1))
{
result=result.add(fun(i));
}
return result;
}
public static void main(String args[])
{
Scanner cin = new Scanner(System.in);
int i = cin.nextInt();
BigInteger n = BigInteger.valueOf(i);
System.out.println(fun(i));
}
}

解决方案 »

  1.   

    BigInteger 也不够用的,使用StringBuffer吧
      

  2.   

    BigInteger是够用的你代码有错误 static BigInteger fun1(BigInteger n) {
    BigInteger i;
    BigInteger result = BigInteger.ONE;
    for (i = BigInteger.ONE; !i.equals(n); i = i.add(BigInteger.ONE)) {
    result = result.multiply(i);
    }
    return result;
    } static BigInteger fun(BigInteger n) {
    BigInteger result = BigInteger.ZERO;
    BigInteger i = BigInteger.valueOf(1);
    for (; !i.equals(n); i = i.add(BigInteger.ONE)) {
    result = result.add(fun1(i));//这里应该是fun1
    }
    return result;
    } public static void main(String args[]) {
    Scanner cin = new Scanner(System.in);
    int i = cin.nextInt();
    BigInteger n = BigInteger.valueOf(i);
    System.out.println(fun(BigInteger.valueOf(i)));
    }
      

  3.   

    而且应该是i = i.add..这样,因为add方法不影响对象本身,是生成新对象的。而且应该尽量使用i.equals(n)就可以。不需要减后再判断等于0,这样没有必要
      

  4.   

    http://ethen.iteye.com/admin/blogs/993101
    推荐参考下!
      

  5.   

    额,原来阶乘后面还要累加。那就直接用biginteger或者bigdecimal吧,应该都可以的。
      

  6.   

    public static void main(String args[]){
    int n=Integer.parseInt(args[0]);
    long starttime = System.currentTimeMillis();
    BigInteger b = new BigInteger("1");
    BigInteger sum = new BigInteger("0");
    for (int i = 1; i < n; i++) {
    BigInteger c = new BigInteger("" + i + "");
    b = b.multiply(c);
    System.out.println(b);
    sum=sum.add(b);
    }
    System.out.println(b);
    System.out.println(sum);
    long endtime = System.currentTimeMillis();
    System.out.println((endtime - starttime) + "(ms)");
    }
      

  7.   


    惭愧,真没试过,只用StringBuffer做过,看来只是计算效率的差别
      

  8.   

    确实是够的, 由Stirling公式可知: log10(1000!) 不到3000.