JAVA Chapter 3 Programming Homework

The third chapter of JAVA programming homework:

public class TestWork {

    public static void main(String args[]){
        System.out.println(trans(64));
        sumNum(100);
        System.out.println(sum);
        fiBo(40,1,1);

    }

    static long sum = 0;

    // 十进制转换二进制
    static long trans(int n){
       int x = n % 2;
       return n > 1 ? (trans(n/2)*10+x) : n;
    }

    // 编程求:∑1+∑2+……+∑100
    static void sumNum(int n){
        int num = 0;
        for (int i=1;i<=n;i++){
            num += i;
            sum += num;
        }
    }

    // 编写递归算法程序:一列数的规则如下: 1,1,2,3,5,8,13,21,34....求数列的前40个
    static void fiBo(int n, int x, int y){
        System.out.println(x);
        if(n>1){
            fiBo(n-1,y,x+y);
        }
    }

    /*static void fiBo(int n){
        int x = 1;
        int y = 1;
        int t = 0;
        for (int i = 1; i <= n; i++) {
            System.out.println(x);
            t = y;
            y = x + y;
            x = t;
        }
    }*/
}

Guess you like

Origin blog.csdn.net/qq_46456049/article/details/108523278