java例题_21 求 1+2!+3!+...+20!的和

 1 /*21 【程序 21 求阶乘】 
 2 题目:求 1+2!+3!+...+20!的和  
 3 程序分析:此程序只是把累加变成了累乘。
 4 */
 5 
 6 /*分析
 7  * 1、汲取上一题的教训,这么大的数字,long类型
 8  * 2、for循环,两层,一层控制1~20,另一层控制阶乘
 9  * */
10 
11 package homework;
12 
13 public class _21 {
14 
15     public static void main(String[] args) {
16         //声明一个long类型的和S,中间值每项值x
17         long s=0;
18         long x=1;
19         for (int i=1; i<=20; i++) {
20             for (int j=i; j>0; j--) {
21                 x=x*j;
22             }
23             s=s+x;
24 //            System.out.println(x+"\t"+s);
25             x=1;  //x复位
26         }
27         System.out.println("和为:"+s);
28 
29     }
30 
31 }

猜你喜欢

转载自www.cnblogs.com/scwyqin/p/12305530.html