1!+3!+5!+...+99!的阶乘

java求1!+3!+5!+...+99!的阶乘的两种方法

方法一: n!=1×2×3×……×n方法

package other;

import java.math.BigDecimal;

public class JieChen {

 /**
  * 1!+3!+5!+...+99!的阶乘为
  * n!=1×2×3×……×n方法
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  int num = 1;
  double count = 0, t = 1; //值太大,用double的科学计算法表示
  while (num<100) {
   for (int i = 1; i <= num; i++) {
    t*=i;  //n!=1×2×3×……×n
   }
   System.out.println(num+" 的阶乘为:" + t);
   count += t;
   t = 1;
   num += 2;
  }
  System.out.println("1!+3!+5!+...+99!的阶乘为:" + count);
  System.out.println(new BigDecimal(count)); //完整的将科学计算法的数字显示出来

 }

}

方法二:n×(n-1)!方法

 

package other;

import java.math.BigDecimal;

public class JieChen4 {

 /**
  * 1!+3!+5!+...+99!的阶乘为
  * n×(n-1)!方法
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  int num = 1;
  double count = 1, t=1; //值太大,用double的科学计算法表示
  while (num<100) {
   if (num>1) {
    t*=(num-1)*num; //n×(n-1)!
   }
   System.out.println(num+" 的阶乘为:" + t);
   count += t;
   num += 2;
  }
  System.out.println("1!+3!+5!+...+99!的阶乘为:" + count);
  System.out.println(new BigDecimal(count)); //完整的将科学计算法的数字显示出来

 }

}



----个人百度空间,http://hi.baidu.com/12365484/item/24b01901f33a93fef45ba68f
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值