Java 實現 C 語言經典 100 例(26 - 30)

1. 實例 26

1.1 題目

利用遞歸方法求5!

1.2 思路

  1. x=01x = 0、1 時,0!=11!=10!=1,1!=1
  2. x>1x > 1 時,x!=(x1)!xx ! = (x-1)!*x

1.3 代碼

/**
 * @ClassName : TwentySix
 * @Author : cunyu
 * @Date : 2020/6/23 20:51
 * @Version : 1.0
 * @Description : 實例 26
 **/

public class TwentySix {
    public static void main(String[] args) throws Exception {
        for (int i = 0; i < 6; i++) {
            System.out.format("%d! = %d\n", i, fact(i));
        }
    }

    /**
     * @param n 整數 n
     * @return 整數 n 的階乘
     * @Description 求 n 的階乘
     * @date 2020/6/23 20:54
     * @author cunyu1943
     * @version 1.0
     */
    public static int fact(int n) {
        int sum;
        if (n == 1 || n == 0) {
            sum = 1;
        } else {
            sum = n * fact(n - 1);
        }
        return sum;
    }
}

1.4 結果

0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120

2. 實例 27

2.1 題目

將所輸入的5個字符,以相反順序打印出來。

2.2 思路

見代碼註釋即可

2.3 代碼

import java.util.Scanner;

/**
 * @ClassName : TwentySeven
 * @Author : cunyu
 * @Date : 2020/6/23 22:58
 * @Version : 1.0
 * @Description : 實例 27
 **/

public class TwentySeven {
    public static void main(String[] args) throws Exception {
        Scanner input = new Scanner(System.in);
        System.out.println("輸入五個字符");
        // 將輸入的字符串轉換爲字符數組
        char[] charArray = input.next().toCharArray();
        System.out.println("逆序輸出");
        // 逆序輸出
        for (int i = charArray.length - 1; i >= 0; i--) {
            System.out.format("%c", charArray[i]);
        }
    }
}

2.4 結果

輸入五個字符
qwert
逆序輸出
trewq

3. 實例 28

3.1 題目

有5個人坐在一起,問第五個人多少歲?他說比第4個人大2歲。問第4個人歲數,他說比第3個人大2歲。問第三個人,又說比第2人大兩歲。問第2個人,說比第一個人大兩歲。最後問第一個人,他說是10歲。請問第五個人多大?

3.2 思路

假設第 5 個人爲 xx 歲,則第 4 個人爲 x2x-2 歲,第 3 個人爲 x22x - 2-2 歲,以此類推,第一個人應該是 x42x - 4 * 2 歲,而第一個人爲 10 歲,所以 x42=10x - 4*2=10

3.3 代碼

/**
 * @ClassName : TwentyEight
 * @Author : cunyu
 * @Date : 2020/6/23 21:33
 * @Version : 1.0
 * @Description : 實例 28
 **/

public class TwentyEight {
    public static void main(String[] args) throws Exception {
        int age;
        age = 4 * 2 + 10;
        System.out.println("第 5 個人年齡是:" + age);
    }
}

3.4 結果

第 5 個人年齡是:18

4. 實例 29

4.1 題目

給一個不多於5位的正整數,要求:一、求它是幾位數,二、逆序打印出各位數字。

4.2 思路

對數進行分解,然後再根據各位結果進行判斷

4.3 代碼

import java.util.Scanner;

/**
 * @ClassName : TwentyNine
 * @Author : cunyu
 * @Date : 2020/6/23 22:37
 * @Version : 1.0
 * @Description : 實例 29
 **/

public class TwentyNine {
    public static void main(String[] args) throws Exception {
        int a, b, c, d, e;
        Scanner input = new Scanner(System.in);
        System.out.println("輸入不多於 5 位的正整數");
        int num = input.nextInt();

        // 分解出個十百千萬位
        a = num / 10000;
        b = num % 10000 / 1000;
        c = num % 1000 / 100;
        d = num % 100 / 10;
        e = num % 10;
        if (a != 0) {
            System.out.format("5 位數,逆序 %d%d%d%d%d\n", e, d, c, b, a);
        } else if (b != 0) {
            System.out.format("4 位數,逆序 %d%d%d%d\n", e, d, c, b);
        } else if (c != 0) {
            System.out.format("3 位數,逆序 %d%d%d\n", e, d, c);
        } else if (d != 0) {
            System.out.format("2 位數,逆序 %d%d%\n", e, d);
        } else {
            System.out.format("1 位數,逆序 %d%\n", e);
        }
    }
}

4.4 結果

輸入不多於 5 位的正整數
56324
5 位數,逆序 42365

5. 實例 30

5.1 題目

一個5位數,判斷它是不是迴文數。即12321是迴文數,個位與萬位相同,十位與千位相同。

5.2 思路

類似於實例 29,將一個數進行分解出各位

5.3 代碼

import java.util.Scanner;

/**
 * @ClassName : Thirty
 * @Author : cunyu
 * @Date : 2020/6/23 22:47
 * @Version : 1.0
 * @Description : 實例 30
 **/

public class Thirty {
    public static void main(String[] args) throws Exception {
        Scanner input = new Scanner(System.in);
        System.out.println("輸入一個 5 位數");
        int num = input.nextInt();
        // 分解出個十百千萬位
        int a, b, c, d, e;
        a = num / 10000;
        b = num % 10000 / 1000;
        c = num % 1000 / 100;
        d = num % 100 / 10;
        e = num % 10;

        if (a == e && b == d) {
            System.out.println("是迴文數");
        } else {
            System.out.println("不是迴文數");
        }
    }
}

5.4 結果

輸入一個 5 位數
12321
是迴文數

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章