【第三周】第三章习题3.7

来源:互联网 发布:js去掉前面的空格 编辑:程序博客网 时间:2024/05/17 21:07
/*2016.9.15 * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. *//** *编写一个应用程序求满足1+2!+3!……+N!《=9999的最大整数N *  * @author 马康泰 */public class Main {    public static void main(String args[]){        int sum=0,fac=0;        int n=1;        Factorial a=new Factorial();        while(sum<=9999){            fac=a.factorial(n);            sum=sum+fac;            n++;        }   System.out.println(n);    }} * To change this template file, choose Tools | Templates * and open the template in the editor. *//** * * @author 马康泰 */public class Factorial {    public int factorial(int i){//阶乘        int j,result=1;        for(j=1;j<=i;j++){            result=result*j;        }        return result;    }}

0 0