//原题:使用BigInteger类计算1!+3!+5!+的前30项的和
import java.math.*;//我想使用BigInteger类来做这个题目,利用BigInteger类中的add(),compareTo(),multiply()方法做,但小子做不出啊,
public class he//我只好用另外一种方法了,只是我觉得我写的这个方法也是不够简洁,也请仙人顺带指点一二,让此方法更加简洁些;
{public static void main(String args[])
{int sum=0,i=1;
while(i<60)
{int a=1;
for(int j=1;j<=i;j++)
{
a=a*j;}
sum=a+sum;
i=i+2;
}
//BigInteger s=new BigInteger("sum");
System.out.println(sum);
}}
import java.math.*;//我想使用BigInteger类来做这个题目,利用BigInteger类中的add(),compareTo(),multiply()方法做,但小子做不出啊,
public class he//我只好用另外一种方法了,只是我觉得我写的这个方法也是不够简洁,也请仙人顺带指点一二,让此方法更加简洁些;
{public static void main(String args[])
{int sum=0,i=1;
while(i<60)
{int a=1;
for(int j=1;j<=i;j++)
{
a=a*j;}
sum=a+sum;
i=i+2;
}
//BigInteger s=new BigInteger("sum");
System.out.println(sum);
}}
解决方案 »
- jdbc连接数据库问题
- 字符串数字排序
- 求救 java解析html
- jsp:plugin codebase 的相对路径应该怎么写,用这样<%= request.getContextPath()%>+folderName又不行
- 请教GIS最短路径输出。。。。。
- 怎么包错不呢》?
- swing中如何限制文本输入的长度?
- 问个关于String的很简单却很困扰人的问题
- 为什么需要拖动一下才会显示窗口中的控件?
- 请问我该学什么了
- java.net.MalformedURLException: no protocol:错误,请高手指点迷津
- JComboBox 事件 怎么只由最开始和最终选中的值来变化,中间过程不变
import java.math.*;public class FactorialThrity { public static void main(String[] args) {
BigInteger sum=new BigInteger("0");
BigInteger peersum=new BigInteger("1");
boolean b=true;
for(int i=1;i<6;i++){
peersum=peersum.multiply(new BigInteger(Integer.toString(i)));
if(b){
sum=sum.add(peersum);
}
b=!b;
}
System.out.println(sum);
}}
大三要学java,正式接触编程,大一,大二都是基础科,暑假了,我想先在家里面练练手,这些题都是课后习题(我在学校买的别人的一本旧书)
import java.math.*;//我想使用BigInteger类来做这个题目,利用BigInteger类中的add(),compareTo(),multiply()方法做,但小子做不出啊,
public class he//我只好用另外一种方法了,只是我觉得我写的这个方法也是不够简洁,也请仙人顺带指点一二,让此方法更加简洁些;
{ public static void main(String args[]) {
BigInteger sum=new BigInteger("0");
int i=30;//三十项
Integer j=1;//当前项
while(0<i--){
sum=sum.add(factorial(new BigInteger(j.toString())));
j+=2;
}
System.out.println(sum);
}
public static BigInteger factorial(BigInteger bint)
{
if( bint.equals(BigInteger.ZERO)||bint.equals(BigInteger.ONE))
return BigInteger.ONE;
return bint.multiply(factorial(bint.subtract(BigInteger.ONE)));
}
}