首页 > 学院 > 开发设计 > 正文

递归函数

2019-11-15 01:05:35
字体:
来源:转载
供稿:网友
递归函数
/* *      递归函数: *  递归函数前十位的和?*/public class text{    public static void main(String[] args){        mm(10);//调用mm方法    }    public static int mm (int n){        if(n==1){//第一位输出为0            return 0;        }        if(n==2){//第二位输出为1------之后的每一位都是前两位之和            return 1;        }        int a=0;        int b=1;        int c=0;        int d=0;        for ( int j =3;j<=10;j++ ){            c=a+b;//a+b的值赋给class            a=b;//b的值赋给a            b=c;//c的值赋给b            d+=c;            System.out.PRintln( a+"+"+b+"," );        }        System.out.println( d+mm(1)+mm(2) );//这里需要加上n==1的值和n==2的值;        return c;    }}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表