Java 遞歸 常見24道題目 總結


1.N個台階的走法遞歸【這里設為10個台階】

 /**
     * N個台階的走法遞歸
     * <p>
     * 有個樓梯,台階有10個,每次可以跳上1階 或者 2階 ,那么台階的走法一共有多少種
     */
    @Test
    public void t() {
        System.out.println(f(10));
    }

    //斐波那契數列變形,求N個台階的走法,遞歸方法
    public int f(int n) {
        if (n <= 2) {
            return n;
        }
        return f(n - 1) + f(n - 2);
    }
View Code

 2.文件刪除遞歸

 /**
     * 文件刪除遞歸
     * <p>
     * 1:檢測文件夾是否存在,不存在則退出
     * 2:獲取該文件夾目錄【獲取文件數組】,遍歷文件數組
     * 3:判斷是文件則刪除,不是則回調 2
     * 4.文件刪除完后,將該文件夾刪除
     */
    @Test
    public void t2() {
        this.delete(file);
    }

    //封裝根目錄
    static File file = new File("C:/Users/cen/Desktop/ww");

    public void delete(File f) {
        if (!f.exists()) {
            System.out.println("該文件夾不存在");
            return;
        }
        //獲取該文件夾的目錄文件數組
        File[] files = f.listFiles();
        //遍歷
        for (File mf : files) {
            //如果是文件夾
            if (mf.isDirectory()) {
                //遞歸
                delete(mf);
            }
            //如果是文件
            else if (mf.isFile()) {
                //刪除文件,方法.delete() 刪除成功返回true ,失敗為false
                System.out.println("文件" + mf.getName() + "被刪除:" + mf.delete());
            }
        }
        System.out.println("最后操作完畢,已經是個空文件夾=" + f.getName() + "=啦,現在刪除該空文件夾:" + f.delete());
    }
View Code

 

3.文件查詢遞歸

/**
     * 文件查詢遞歸
     * <p>
     * 1:檢測文件夾是否存在,不存在則退出
     * 2:獲取該文件夾目錄【獲取文件數組】,遍歷文件數組
     * 3:判斷是文件夾則回調 2  ,如果是是文件且文件名后綴是 .java 則打印絕對路徑
     */
    @Test
    public void t3() {
        this.find(file2);
    }

    //封裝根目錄
    static File file2 = new File("C:/Users/cen/Desktop/ww");

    public void find(File f) {
        if (!f.exists()) {
            System.out.println("該文件夾不存在");
            return;
        }
        //獲取該文件夾的目錄文件數組
        File[] files = f.listFiles();
        //遍歷
        if (files != null) {
            for (File mf : files) {
                //如果是文件夾
                if (mf.isDirectory()) {
                    //遞歸
                    find(mf);
                }
                //如果是文件 且文件名后綴是 .java 則打印覺得路徑
                else if (mf.isFile() && mf.getName().endsWith(".java")) {
                    //刪除文件,方法.delete() 刪除成功返回true ,失敗為false
                    System.out.println("文件" + mf.getName() + "絕對路徑:" + mf.getAbsolutePath());
                }
            }
        }
    }
View Code

 

 

 

4.使用遞歸方式計算 隨機數 [1,10]的乘階結果並打印過程

 /**
     * 使用遞歸方式計算 隨機數 [1,10]的乘階結果並打印過程
     */
    @Test
    public void t4() {
//        (數據類型)(最小值+Math.random()*(最大值-最小值+1))
        int i = (int) (1 + Math.random() * (10 - 1 + 1));
        System.out.println(i + "的乘階是:" + this.mo(i));
        StringBuilder s = new StringBuilder();
        for (int n : list) {
            s.append(n).append("*");
        }
        int len = s.length();
        //去掉末尾的 *號
        //substrings是要包含末尾索引的字符的
        String str = s.substring(0, len - 1);
        System.out.println("乘階過程是" + str);
    }

    //記錄數字
    List<Integer> list = new ArrayList<>();

    public int mo(int n) {
        //記錄數字
        list.add(n);
        if (n < 2) {
            //n為1
            return n;
        }
        //遞歸
        return n * mo(n - 1);
    }
View Code

 

 

 5.有一對兔子,從出生后第三個月起每個月都生一對兔子 ,小兔子長到第三個月后每

  個月又生一對兔子,假如兔子都不死 ,問每個月的兔子總數為多少?

    /**
     * 題目:有一對兔子,從出生后第三個月起每個月都生一對兔子 ,小兔子長到第三個月后每個月又生一對兔子,假如兔子都不死 ,問每個月的兔子總數為多少?
     * <p>
     * 解析:使用斐波拉契數列計算 : 每一項都是前兩項之和,一般使用遞歸方法計算
     * <p>
     * 兔子數列應該是 1,1,2,3,5,8,13,21,34 【注意:這里的單位是對 ,那么兔子數需要 * 2】
     */
    @Test
//    public void t5() {
    public static void main(String[] args) {
        //獲取月份數
        int n = getMonth();
        System.out.println("當前輸入月份是:" + n);
        //兔子對數
        int xx = tutu(n);
        System.out.println("當前兔子對數是:" + xx);
        System.out.println("當前共有" + 2 * xx + "只兔子");
    }

    //獲取鍵盤數字遞歸
    public static int getMonth() {
        System.out.println("請輸入月份【正整數】:");
        // 從鍵盤接收數據
        Scanner scanner = new Scanner(System.in);
        //判斷是否有輸入值
        if (scanner.hasNext()) {
            //接收數值
            String m = scanner.next();
            try {
                //字符串轉整數
//                Integer x = Integer.valueOf(m);
                int x = Integer.parseInt(m);
                if (x > 0) {
                    return x;
                }
                return getMonth();
            } catch (Exception e) {
                return getMonth();
            }
        }
        return getMonth();
    }


    //斐波拉契數列遞歸
    public static int tutu(int n) {
        //前兩個月不生兔子,還是一對
        if (n < 3) {
            return 1;
        } else {
            return tutu(n - 1) + tutu(n - 2);
        }
    }
View Code

 

 

 

 6.判斷101-200之間有多少個素數,並輸出所有素數

    /**
     * 判斷101-200之間有多少個素數,並輸出所有素數
     * 【1不是素數】因此最少2開始
     */
    //非遞歸方式求解
    @Test
    public void t6() {
        Map<Integer, Object> map = new HashMap<>();
        int n = 101;
        int j = 200;
        for (int i = n; i <= j; i++) {
            List list = new ArrayList();
            int k = 1;
            while (k < i) {
                //整除,余數為0
                if (i % k == 0) {
                    list.add(k);
                }
                k++;
            }
            if (list.size() == 1 && (Integer) list.get(0) == 1) {
                //只有一個因數 1 才可以
                map.put(i, list);
            }
        }
        //
        System.out.println("共有" + map.size() + "個素數");
        System.out.println("直接map打印,無序的");
        System.out.println(map.keySet());
        List<Integer> list2 = new ArrayList<>();
        //升序
        for (Integer x : map.keySet()) {
            list2.add(x);
        }
        System.out.println("lambda表達式排序[升序]");
        //lambda表達式排序
        list2.sort((n1, n2) -> n1 - n2);
        System.out.println(list2);
    }
View Code

 

 

 7.打印出所有的 "水仙花數 ",所謂 "水仙花數 "是指一個三位數,其各位數字立方和等於該數本身。

例如:153是一個 "水仙花數 ",因為153=1的三次方+5的三次方+3的三次方

    /**
     * 打印出所有的 "水仙花數 ",所謂 "水仙花數 "是指一個三位數,其各位數字立方和等於該數本身。例如:153是一個 "水仙花數 ",因為153=1的三次方+5的三次方+3的三次方
     */
    //非遞歸方法求解
    @Test
    public void t7() {
        int n = 100;
        int j = 999;
        for (int i = n; i <= j; i++) {
            this.sxh(i);
        }
    }

    //整數
    int aaa, aa, a;

    public void sxh(int num) {
        //因為是整數型類,只會保留整數部分
        aaa = num / 100;
        //num%100取余數部分,然后除以10,再次取整數部分
        aa = (num % 100) / 10;
        a = num % 10;
        if (aaa * aaa * aaa + aa * aa * aa + a * a * a == num) {
            System.out.println(num);
        }

    }
View Code

 

 

 

 8.將一個正整數分解 質因數 。例如:輸入90,打印出90=2*3*3*5

 /**
     * 將一個正整數分解 質因數 。例如:輸入90,打印出90=2*3*3*5
     */
    @Test
    public void t8() {
        int n = 90;
        s.append(n).append("=");
        this.fjys(n);
        System.out.println(s);

    }

    StringBuilder s = new StringBuilder();

    int k = 2;

    public void fjys(int n) {
        if (k <= n) {
            if (k < n && n % k == 0) {
                //說明可以整除
                s.append(k).append("*");
                //剩下部分
                int y = n / k;
                //遞歸
                fjys(y);
            } else if (k < n && n % k != 0) {
                //說明不可以整除,不是因數
                //需要加一
                k++;
                //遞歸
                fjys(n);
            } else if (k == n) {
                //說明已經遍歷到剩余數字本身,結束啦
                s.append(n);
            }
        }

    }
View Code

 

 

 

 9.利用條件運算符的嵌套來完成此題:學習成績> =90分的同學用A表示,60-89分之間的用B表示,60分以下的用C表示

/**
     * 利用條件運算符的嵌套來完成此題:學習成績> =90分的同學用A表示,60-89分之間的用B表示,60分以下的用C表示
     */
    @Test
    public void t9() {
        System.out.println("成績93-》" + ys(93));
        System.out.println("成績89-》" + ys(89));
        System.out.println("成績35-》" + ys(35));
    }

    public char ys(int n) {
        //注意啦。必須使用單引號
        return n >= 90 ? 'A' : (n < 60 ? 'C' : 'B');
    }
View Code

 

 

 

 10.輸入兩個正整數m和n,求其最大公約數和最小公倍數

   /**
     * 輸入兩個正整數m和n,求其最大公約數和最小公倍數
     * <p>
     * 整數乘積 除以 最大公因數 則等於 最小公倍數
     */

    @Test
//    public void t10() {
    public static void main(String[] args) {
        Scanner s1 = new Scanner(System.in);
        System.out.println("請輸入第一個正整數");
        int m = s1.nextInt();
        Scanner s2 = new Scanner(System.in);
        System.out.println("請輸入第二個正整數");
        int n = s2.nextInt();
        if (n > m) {
            int t = m;
            m = n;
            n = t;
        }
        int bb = yb(n, m);
        System.out.println("最大公約數是:" + bb);
        System.out.println("最小公倍數是:" + m * n / bb);
    }

    //遞歸,計算最大共因素
    //參數 n 必須比m 小
    public static int yb(int n, int m) {
        int temp;
        if (m % n == 0) {
            temp = n;
        } else {
            //遞歸
            temp = yb(m % n, n);
        }
        return temp;
    }
View Code

 

 

 

 11.輸入一行字符,分別統計出其中 英文字母 、 空格 、 數字 和 其它字符 的個數

  /**
     * 輸入一行字符,分別統計出其中 英文字母 、 空格 、 數字 和 其它字符 的個數
     */
    @Test
//    public void t11() {
    public static void main(String[] args) {
        Scanner s1 = new Scanner(System.in);
        System.out.println("請輸入字符串");
        //.nextLine()表示輸入一行字符,允許接收空格
        String m = s1.nextLine();
        System.out.println("字符總數:" + m.length());
        char[] ch = m.toCharArray();
        //英文字母
        int a = 0;
        //空格
        int b = 0;
        //數字
        int c = 0;
        //其它字符
        int d = 0;
        for (char n : ch) {
            //對比 ASCLL 碼
            if (('a' <= n && n <= 'z') || ('A' <= n && n <= 'Z')) {
                a++;
            } else if (n == ' ') {
                b++;
            } else if ('0' <= n && n <= '9') {
                c++;
            } else {
                d++;
            }
        }
        System.out.println("英文字母" + a);
        System.out.println("空格" + b);
        System.out.println("數字" + c);
        System.out.println("其它字符" + d);


    }
View Code

 

 

 

 12.求s=a+aa+aaa+aaaa+aa...a的值,其中a是一個數字。例如2+22+222+2222+22222(此時共有5個數相加),幾個數相加有鍵盤控制

    /**
     * 求s=a+aa+aaa+aaaa+aa...a的值,其中a是一個數字。例如2+22+222+2222+22222(此時共有5個數相加),幾個數相加有鍵盤控制
     */
    @Test
//    public void t12() {
    public static void main(String[] args) {
        Scanner s1 = new Scanner(System.in);
        System.out.println("請輸入一個數字");
        int a = s1.nextInt();
        if (a < 0) {
            System.out.println("不允許為負數");
            return;
        } else if (a == 0) {
            System.out.println("結果為0");
            return;
        }
        Scanner s2 = new Scanner(System.in);
        System.out.println("請輸入相加的數字個數");
        int n = s2.nextInt();
        if (n < 0) {
            System.out.println("不允許為負數");
            return;
        } else if (n == 0) {
            System.out.println("結果為0");
            return;
        }
        //
        StringBuilder stringBuilder = new StringBuilder();
        int res = 0;
        //循環 n 加法運算
        for (int i = 1; i <= n; i++) {
            //存儲字符串
            StringBuilder resstr = new StringBuilder();
            //拼接字符串
            for (int k = 0; k < i; k++) {
                resstr.append(a);
            }
            //記錄
            stringBuilder.append(resstr).append("+");
            //字符串轉整數后做加法運算
            res += Integer.parseInt(resstr + "");
        }
        //去掉末尾 + 號
        String f = stringBuilder.substring(0, stringBuilder.length() - 1);
        //打印
        System.out.println(f + "=" + res);
    }
View Code

 

 

 

 13.一個數如果恰好等於它的因子之和,這個數就稱為 "完數 "。例如6=1+2+3.編程找出1000以內的所有完數

 /**
     * 一個數如果恰好等於它的因子之和,這個數就稱為 "完數 "。例如6=1+2+3.編程找出1000以內的所有完數
     * <p>
     * 注意,這是因子,也叫因數 ,是指 -》整數a除以整數b(b≠0) 的商bai正好是整數而du沒有余數,我們就說b是a的因數
     * 【90 = 10 * 9】
     * <p>
     * 與質因數【質因子】不同,【90=2*3*3*5】
     */
    @Test
    public void t13() {

        for (int i = 1; i < 1000; i++) {
            int count = 0;
            for (int j = 1; j <= i / 2; j++) {
                if (i % j == 0) {
                    count += j;
                }
            }
            if (count == i) {
                System.out.println(i);
            }
        }
    }
View Code

 

 

14.一球從100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在 第10次落地時,共經過多少米?第10次反彈多高?

  /**
     * 一球從100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在 第10次落地時,共經過多少米?第10次反彈多高?
     */
    @Test
    public void t14() {
        double h = 100.0;
        double s = 0;
        for (int i = 0; i < 10; i++) {
            System.out.print("第" + (i + 1) + "次落下時候高度:" + h + "米 ");
            s += h + h / 2;
            h = h / 2;
            System.out.print("反彈高度為" + h + "米 ");
            System.out.println("抵達反彈高度時球共經過" + s + "米");
        }
        s -= h;
        System.out.println("第10次落地時,共經過" + s + "米");
        System.out.println("第10次反彈" + h + "米");

    }
View Code

 

 

 

 15.有1、2、3、4四個數字,能組成多少個互不相同且一個數字中無重復數字的三位數?並把他們都輸入

    /**
     * 有1、2、3、4四個數字,能組成多少個互不相同且一個數字中無重復數字的三位數?並把他們都輸入
     */
    @Test
    public void t15() {
        int[] arr = {1, 2, 3, 4};
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length; j++) {
                for (int k = 0; k < arr.length; k++) {
                    if (arr[i] != arr[j] && arr[i] != arr[k] && arr[j] != arr[k]) {
                        int n = arr[i] * 100 + arr[j] * 10 + arr[k];
                        System.out.print(n+"  ");
                        count++;
                    }
                }
            }
        }
        System.out.println();
        System.out.println("總共個數:" + count);
    }
View Code

 

 

 

 16. 企業發放的獎金根據利潤提成。利潤(I)低於或等於10萬元時,獎金可提10%;利潤高於10萬元,低於20萬元時,低於10萬元的部分按10%提成,

  高於10萬元的部分,可可提成7.5%;20萬到40萬之間時,高於20萬元的部分,可提成5%;40萬到60萬之間時高於40萬元的部分,可提成3%;

  60萬到100萬之間時,高於60萬元的部分,可提成1.5%,高於100萬元時,超過100萬元的部分按1%提成,從鍵盤輸入當月利潤,求應發放獎金總數?

  /**
     * 企業發放的獎金根據利潤提成。利潤(I)低於或等於10萬元時,獎金可提10%;
     * 利潤高於10萬元,低於20萬元時,低於10萬元的部分按10%提成,高於10萬元的部分,可可提成7.5%;
     * 20萬到40萬之間時,高於20萬元的部分,可提成5%;
     * 40萬到60萬之間時高於40萬元的部分,可提成3%;
     * 60萬到100萬之間時,高於60萬元的部分,可提成1.5%,
     * 高於100萬元時,超過100萬元的部分按1%提成,
     * 從鍵盤輸入當月利潤,求應發放獎金總數?
     */
    @Test
    public void t16() {
//    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("請輸入利潤【正數】");
        double li = scanner.nextDouble();
        if (li < 0) {
            System.out.println("不可輸入負數");
            return;
        }

        double m1, m2, m3, m4, m5;
        //10萬
        m1 = 10 * 10000 * 0.1;
        //20萬
        m2 = m1 + 10 * 10000 * 0.075;
        //40萬
        m3 = m2 + 20 * 10000 * 0.05;
        //60萬
        m4 = m3 + 20 * 10000 * 0.03;
        //100萬
        m5 = m4 + 40 * 10000 * 0.015;

        double count = 0;
        if (li <= 10 * 10000) {
            count = 0.1 * li;
        } else if (li <= 20 * 10000) {
            count = m1 + (li - 10 * 10000) * 0.075;
        } else if (li <= 40 * 10000) {
            count = m2 + (li - 20 * 10000) * 0.05;
        } else if (li <= 60 * 10000) {
            count = m3 + (li - 40 * 10000) * 0.03;
        } else if (li <= 100 * 10000) {
            count = m4 + (li - 60 * 10000) * 0.015;
        } else {
            count = m5 + (li - 100 * 10000) * 0.01;
        }
        System.out.println("應發放獎金總數:" + count + "元");
    }
View Code

 

 

 

17.一個整數,它加上100后是一個完全平方數,再加上168又是一個完全平方數,請問該數是多少?

**
     * 一個整數,它加上100后是一個完全平方數,再加上168又是一個完全平方數,請問該數是多少?
     */
    @Test
    public void t17() {
        boolean k = true;
        int i = 0;
        while (k == true) {
            int t = i + 100;
            //開平方根
            double c1 = Math.sqrt(t);
            //余數為0,說明平方根是整數,即該數為完全平方數
            if (c1 % 1 == 0) {
                int t2 = i + 168;
                //開平方根
                double c2 = Math.sqrt(t2);
                //余數為0,說明平方根是整數,即該數為完全平方數
                if (c2 % 1 == 0) {
                    System.out.println(i);
                    k = false;
                }
            }
            i++;
        }
    }
View Code

 

 

 18.輸入某年某月某日,判斷這一天是這一年的第幾天?

   /**
     * 輸入某年某月某日,判斷這一天是這一年的第幾天?
     */
    @Test
//    public void t18() {
    public static void main(String[] args) {
        //閏年
        int[] leapYear = {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366};
        //平年
        int[] commonYear = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
        Scanner input = new Scanner(System.in);
        System.out.print("請輸入您要查詢的年份:");
        int year = input.nextInt();
        System.out.print("請輸入您要查詢的月份:");
        int month = input.nextInt();
        System.out.print("請輸入您要查詢的天數:");
        int day = input.nextInt();
        int sumDays = 0;
        String type = "";
        if (month > 12 || month < 1) {
            System.out.println("請輸入正確的月份!");
        } else {
            //判斷是否為閏年
            //普通年能被4整除且不能被100整除的為閏年。(如2004年就是閏年,1900年不是閏年)
            //世紀年能被400整除的是閏年。(如2000年是閏年,1900年不是閏年)
//            閏年的二月為29天,平年的為28天
            if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
                //閏年
                type = "閏年";
                //天數為31天的月份
                if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
                    //判斷天數是否在31天之內
                    if (day < 1 || day > 31) {
                        System.out.println("請輸入正確的天數!");
                    } else {
                        sumDays = leapYear[month - 1] + day;
                    }
                } else if (month == 2) {
                    //判斷天數是否在29天之內
                    if (day < 1 || day > 29) {
                        System.out.println("請輸入正確的天數!");
                    } else {
                        sumDays = leapYear[month - 1] + day;
                    }
                } else {//為4、6、9、11月中的一月
                    //判斷天數是否在30天之內
                    if (day < 1 || day > 30) {
                        System.out.println("請輸入正確的天數!");
                    } else {
                        sumDays = leapYear[month - 1] + day;
                    }
                }
            } else {
                //為平年
                type = "平年";
                //天數為31天的月份
                if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
                    //判斷天數是否在31天之內
                    if (day < 1 || day > 31) {
                        System.out.println("請輸入正確的天數!");
                    } else {
                        sumDays = commonYear[month - 1] + day;
                    }
                } else if (month == 2) {
                    //判斷天數是否在28天之內
                    if (day < 1 || day > 28) {
                        System.out.println("請輸入正確的天數!");
                    } else {
                        sumDays = commonYear[month - 1] + day;
                    }
                } else {//為4、6、9、11月中的一月
                    //判斷天數是否在30天之內
                    if (day < 1 || day > 30) {
                        System.out.println("請輸入正確的天數!");
                    } else {
                        sumDays = commonYear[month - 1] + day;
                    }
                }
            }
        }
        System.out.println("這一年是" + type + ",這一天為一年中的第" + sumDays + "天");
    }
View Code

 

 

 

 19.輸入三個整數x,y,z,請把這三個數由小到大輸出

    /**
     * 輸入三個整數x,y,z,請把這三個數由小到大輸出
     */
    @Test
    public void t19() {
//    public static void main(String[] args) {
        System.out.println("輸入三個整數x,y,z");
        Scanner s1 = new Scanner(System.in);
        System.out.println("x=");
        int x = s1.nextInt();
        System.out.println("y=");
        int y = s1.nextInt();
        System.out.println("z=");
        int z = s1.nextInt();
        List<Integer> list = new ArrayList<>();
        list.add(x);
        list.add(y);
        list.add(z);
        //lamda排序,升序
        list.sort((a, b) -> a - b);
        System.out.println(list);
    }
View Code

 

 

 

20.輸出9*9口訣

  /**
     * 輸出9*9口訣
     */
    @Test
    public void t20() {
        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(i + "*" + j + "=" + i * j + "  ");
            }
            System.out.println();
        }
    }
View Code

 

 

 

 21.猴子吃桃問題:猴子第一天摘下若干個桃子,當即吃了一半,還不癮,又多吃了一個 第二天早上又將剩下的桃子吃掉一半,又多吃了一個 。

  以后每天早上都吃了前一天剩下 的一半零一個。到第10天早上想再吃時,見只剩下一個桃子了。求第一天共摘了多少

 /**
     * 猴子吃桃問題:猴子第一天摘下若干個桃子,當即吃了一半,還不癮,又多吃了一個 第二天早上又將剩下的桃子吃掉一半,又多吃了一個
     * 。以后每天早上都吃了前一天剩下 的一半零一個。到第10天早上想再吃時,見只剩下一個桃子了。求第一天共摘了多少
     * <p>
     * [也就是說,一共吃了9天]
     */

    @Test
    public void t21() {
        System.out.println(tao(1));
    }

    //吃的總次數
    int times = 9;

    //遞歸
    public int tao(int n) {
        if (times > 0) {
            times--;
            n += 1;
            n = n * 2;
            n = tao(n);
        }
        return n;
    }
View Code

 

 

 22.兩個乒乓球隊進行比賽,各出三人。甲隊為a,b,c三人,乙隊為x,y,z三人。已抽簽決定比賽名單。

  有人向隊員打聽比賽的名單。a說他不和x比,c說他不和x,z比,請編程序找出三隊賽手的名單

/**
     * 兩個乒乓球隊進行比賽,各出三人。甲隊為a,b,c三人,乙隊為x,y,z三人。已抽簽決定比賽名單。
     * 有人向隊員打聽比賽的名單。a說他不和x比,c說他不和x,z比,請編程序找出三隊賽手的名單
     * <p>
     * //注意,是三隊
     */

    @Test
    public void t22() {
        char[] n = {'x', 'y', 'z'};
        for (char a : n) {
            for (char b : n) {
                for (char c : n) {
                    //取出乙隊三人不重復
                    if (a != b && a != c && b != c) {
                        //當 a不是x ,c 不是 x、z 時
                        if (a != 'x' && c != 'x' && c != 'z') {
                            System.out.println("a<-->" + a);
                            System.out.println("b<-->" + b);
                            System.out.println("c<-->" + c);
                        }
                    }
                }
            }
        }
    }
View Code

 

 

 

 23.打印出如下圖案(菱形)【最難啦】

    /**
     * 打印出如下圖案(菱形)
     * <p>
     * 好難
     */
    @Test
    public void t23() {
        //邊長
        int s = 5;
        //
        int a = s + 1;//6
        int b = s - 1;//4
        int c = 2 * b + a;//14
        //行數/列數
        int d = s * 2 - 1;//9
        //每行
        for (int i = 1; i <= d; i++) {
            //上半部分
            if (i <= b) {
                //每列
                for (int j = 1; j <= d; j++) {
                    if (j == a - i || j == b + i) {
                        System.out.print(" * ");
                    } else {
                        System.out.print("   ");
                    }
                }
            }
            //下半部分
            else {
                //每列
                for (int k = 1; k <= d; k++) {
                    if (k == i - b || k == c - i) {
                        System.out.print(" * ");
                    } else {
                        System.out.print("   ");
                    }
                }
            }
            System.out.println();
        }
    }
View Code

 

 

 24.有一分數序列:2/1,3/2,5/3,8/5,13/8,21/13...求出這個數列的前20項之和

    /**
     * 有一分數序列:2/1,3/2,5/3,8/5,13/8,21/13...求出這個數列的前20項之和
     */
    @Test
    public void t24() {

        int x = 2;
        int y = 1;
        int z ;
        double sum = 0;
        for (int i = 0; i < 20; i++) {
            sum += (double) x / y;
            //備份分母
            z = y;
            //新的分母是前一個的分子
            y = x;
            //新的分子是前一個分子加分母,與 x = x + z; 一樣
            x = y + z;
        }
        System.out.println(sum);
    }
View Code

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM