题目要求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));
}
}
一下是我的代码:哪里有问题啊!
//这个程序那里错了
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));
}
}
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)));
}
推荐参考下!
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)");
}
惭愧,真没试过,只用StringBuffer做过,看来只是计算效率的差别