[Java]Java编程思想(Thinking in Java)第四章习题

Head Pic: 【オリジナル】"亥" / Illustration by "秋赤音" [pixiv]

练习1

写一个程序,打印1到100的值。

package world.pixiv.thinkinginjava.exercise.chapter4;

import static world.pixiv.thinkinginjava.util.Printer.*;

public class Exercise1 {
    public static void main(String[] args) {
        for(int i = 1;i <= 100;i++) {
            print(i + " ");
            if(i % 10 == 0) println();
        }
    }
}
/* Output:
 * 1 2 3 4 5 6 7 8 9 10 
 * 11 12 13 14 15 16 17 18 19 20 
 * 21 22 23 24 25 26 27 28 29 30 
 * 31 32 33 34 35 36 37 38 39 40 
 * 41 42 43 44 45 46 47 48 49 50 
 * 51 52 53 54 55 56 57 58 59 60 
 * 61 62 63 64 65 66 67 68 69 70 
 * 71 72 73 74 75 76 77 78 79 80 
 * 81 82 83 84 85 86 87 88 89 90 
 * 91 92 93 94 95 96 97 98 99 100 
 */

练习2

写一个程序,产生25个int类型的随机数,对于每一个随机值,使用if-else语句来将其分类为大于,小于或者等于紧随它而随机产生的值。

package world.pixiv.thinkinginjava.exercise.chapter4;

import static world.pixiv.thinkinginjava.util.Printer.*;

import java.util.ArrayList;
import java.util.Random;

public class Exercise2 {
    public static void main(String[] args) {
        Random rand = new Random(12361);
        int nums[] = new int[25];
        ArrayList<Integer> greater = new ArrayList<Integer>();
        ArrayList<Integer> less = new ArrayList<Integer>();
        ArrayList<Integer> equal = new ArrayList<Integer>();
        for(int i = 0;i < 25;i++) {
            nums[i] = rand.nextInt(100);
        }
        for(int i = 0;i < 24;i++) {
            if(nums[i] > nums[i + 1]) greater.add(nums[i]);
            else if(nums[i] < nums[i + 1]) less.add(nums[i]);
            else equal.add(nums[i]);
        }
        print("Nums: ");
        for(int i : nums) {
            print(i + " ");
        }
        print("\nGreater: ");
        for(int i : greater) {
            print(i + " ");
        }
        print("\nLess: ");
        for(int i : less) {
            print(i + " ");
        }
        print("\nEqual: ");
        for(int i : equal) {
            print(i + " ");
        }
    }
}
/* Output:
 * Nums: 77 79 2 19 93 12 62 23 8 55 82 22 42 31 7 11 42 80 88 61 90 29 63 0 42 
 * Greater: 79 93 62 23 82 42 31 88 90 63 
 * Less: 77 2 19 12 8 55 22 7 11 42 80 61 29 0 
 * Equal: 
 */

练习3

修改练习2,把代码用一个while无限循环包括起来,然后运行它直至用键盘中断其运行。

package world.pixiv.thinkinginjava.exercise.chapter4;

import static world.pixiv.thinkinginjava.util.Printer.*;

import java.util.ArrayList;
import java.util.Random;

public class Exercise3 {
    public static void main(String[] args) {
        Random rand = new Random(12361);
        int nums[] = new int[25];
        ArrayList<Integer> greater = new ArrayList<Integer>();
        ArrayList<Integer> less = new ArrayList<Integer>();
        ArrayList<Integer> equal = new ArrayList<Integer>();
        while(true) {
            for(int i = 0;i < 25;i++) {
                nums[i] = rand.nextInt(100);
            }
            for(int i = 0;i < 24;i++) {
                if(nums[i] > nums[i + 1]) greater.add(nums[i]);
                else if(nums[i] < nums[i + 1]) less.add(nums[i]);
                else equal.add(nums[i]);
            }
            print("\nNums: ");
            for(int i : nums) {
                print(i + " ");
            }
            print("\nGreater: ");
            for(int i : greater) {
                print(i + " ");
            }
            print("\nLess: ");
            for(int i : less) {
                print(i + " ");
            }
            print("\nEqual: ");
            for(int i : equal) {
                print(i + " ");
            }
            greater.clear();
            less.clear();
            equal.clear();
        }
    }
}
/* Output:
 * Nums: 61 54 84 8 13 54 77 8 73 2 47 83 28 12 97 33 70 73 18 45 77 69 60 63 35 
 * Greater: 61 84 77 73 83 28 97 73 77 69 63 
 * Less: 54 8 13 54 8 2 47 12 33 70 18 45 60 
 * Equal: 
 * Nums: 89 15 8 31 81 20 52 9 13 28 48 88 45 10 88 3 74 32 98 75 21 23 47 73 51 
 * Greater: 89 15 81 52 88 45 88 74 98 75 73 
 * Less: 8 31 20 9 13 28 48 10 3 32 21 23 47 
 * Equal: 
 * Nums: 61 38 35 15 46 10 94 34 14 33 60 81 97 90 35 90 37 76 35 58 72 49 48 83 0 
 * Greater: 61 38 35 46 94 34 97 90 90 76 72 49 83 
 * Less: 15 10 14 33 60 81 35 37 35 58 48 
 * ...
 */

练习4

写一个程序,使用两个嵌套的for循环和取余操作符来探测和打印素数。

package world.pixiv.thinkinginjava.exercise.chapter4;

import static world.pixiv.thinkinginjava.util.Printer.*;

public class Exercise4 {
    public static boolean isPrime(int a) {
        int sqr = (int)Math.sqrt(a) + 1;
        if(a == 0 || a == 1) return false;
        for(int i = 2;i <= sqr;i++) {
            if(a % i == 0) return false;
        }
        return true;
    }
    
    public static void main(String[] args) {
        println(isPrime(12361));
        println(isPrime(97));
    }
}
/* Output:
 * false
 * true
 */

练习5

重复第三章中的练习10,不要用Integer.toBinaryString()方法,而是用三元运算符和按位操作符来显示二进制的0和1。

package world.pixiv.thinkinginjava.exercise.chapter4;

import static world.pixiv.thinkinginjava.util.Printer.*;

public class Exercise5 {
    public static String toBinaryString(int a) {
        if(a == 0) return "0";
        
        String ret = "";
        while(a != 0) {
            ret += ((a & 1) == 1) ? "1" : "0";
            a >>= 1;
        }
        return ret;
    }
    
    public static void main(String[] args) {
        int a = 170;//10101010
        int b = 85;//1010101
        println("a and b: " + toBinaryString(a & b));
        println("a or b: " + toBinaryString(a | b));
        println("a xor b: " + toBinaryString(a ^ b));
    }
}
/* Output:
 * a and b: 0
 * a or b: 11111111
 * a xor b: 11111111
 */

练习6

修改前两个test()方法,让它们接受两个额外的参数begin和end,这样在测试testval时判断它是否在begin和end之间(包括begin和end)的范围内。

package world.pixiv.thinkinginjava.exercise.chapter4;

import static world.pixiv.thinkinginjava.util.Printer.*;

public class Exercise6 {
    public static int test1(int testval,int begin,int end) {
        int result = 0;
        if(testval <= end && testval >= begin) result = 1;
        else result = -1;
        return result;
    }
    
    public static int test2(int testval,int begin,int end) {
        if(testval <= end && testval >= begin) return 1;
        else return -1;
    }
    
    public static void main(String[] args) {
        println("test1: " + test1(666,555,777));
        println("test2: " + test2(12361,12111,12222));
    }
}
/* Output:
 * test1: 1
 * test2: -1
 */

练习7

修改本章练习1,通过使用break关键字,使得程序在打印到99时退出,然后尝试用return来达到同样效果。

package world.pixiv.thinkinginjava.exercise.chapter4;

import static world.pixiv.thinkinginjava.util.Printer.*;

public class Exercise7 {
    public static void main(String[] args) {
        for(int i = 1;i <= 100;i++) {
            print(i + " ");
            if(i % 10 == 0) println();
            if(i == 99) break;
        }
        println();
        for(int i = 1;i <= 100;i++) {
            print(i + " ");
            if(i % 10 == 0) println();
            if(i == 99) return;;
        }
    }
}
/* Output:
 * 1 2 3 4 5 6 7 8 9 10 
 * 11 12 13 14 15 16 17 18 19 20 
 * 21 22 23 24 25 26 27 28 29 30 
 * 31 32 33 34 35 36 37 38 39 40 
 * 41 42 43 44 45 46 47 48 49 50 
 * 51 52 53 54 55 56 57 58 59 60 
 * 61 62 63 64 65 66 67 68 69 70 
 * 71 72 73 74 75 76 77 78 79 80 
 * 81 82 83 84 85 86 87 88 89 90 
 * 91 92 93 94 95 96 97 98 99 
 * 1 2 3 4 5 6 7 8 9 10 
 * 11 12 13 14 15 16 17 18 19 20 
 * 21 22 23 24 25 26 27 28 29 30 
 * 31 32 33 34 35 36 37 38 39 40 
 * 41 42 43 44 45 46 47 48 49 50 
 * 51 52 53 54 55 56 57 58 59 60 
 * 61 62 63 64 65 66 67 68 69 70 
 * 71 72 73 74 75 76 77 78 79 80 
 * 81 82 83 84 85 86 87 88 89 90 
 * 91 92 93 94 95 96 97 98 99 
*/

练习8

写一个switch开关语句,为每个case打印一个消息。然后把这个switch放进for循环来测试每个case,先让每个case后面都有break,测试一下会怎样,然后把break删掉,看看会怎样。

package world.pixiv.thinkinginjava.exercise.chapter4;

import static world.pixiv.thinkinginjava.util.Printer.*;

public class Exercise8 {
    public static void main(String[] args) {
        for(int i = 0;i < 5;i++) {
            switch(i) {
            case 0 : println("0");break;
            case 1 : println("1");break;
            case 2 : println("2");break;
            case 3 : println("3");break;
            default : println("4");break;
            }
        }
        println();
        for(int i = 0;i < 5;i++) {
            switch(i) {
            case 0 : println("0");
            case 1 : println("1");
            case 2 : println("2");
            case 3 : println("3");
            default : println("4");
            }
        }
    }
}
/* Output:
 * 0
 * 1
 * 2
 * 3
 * 4
 * 
 * 0
 * 1
 * 2
 * 3
 * 4
 * 1
 * 2
 * 3
 * 4
 * 2
 * 3
 * 4
 * 3
 * 4
 * 4
 */

练习9

创建一个方法,接受一个整数参数,并显示从第一个元素开始的斐波那契数字,例如执行java Fibonacci 5,输出1,1,2,3,5

package world.pixiv.thinkinginjava.exercise.chapter4;

import static world.pixiv.thinkinginjava.util.Printer.*;

import java.util.Scanner;

public class Exercise9 {
    static int[] f = new int[1000 + 10];
    
    public static void init() {
        f[0] = f[1] = 1;
        for(int i = 2;i < 1000;i++) {
            f[i] = f[i - 1] + f[i - 2];
        }
    }
    
    @SuppressWarnings("resource")
    public static void main(String[] args) {
        init();
        Scanner in = new Scanner(System.in);
        int a = in.nextInt();
        for(int i = 0;i < a;i++) {
            print(f[i] + " ");
        }
    }
}
/* Input:
 * 6
 * Output:
 * 1 1 2 3 5 8 
 */

练习10

下列数字都是“吸血鬼”数字:

$1260=21\times 60$

$1827=21\times 87$

$2187=27\times 81$

写一个程序,找出4位所有吸血鬼数字。

package world.pixiv.thinkinginjava.exercise.chapter4;

import static world.pixiv.thinkinginjava.util.Printer.*;

public class Exercise10 {
    public static boolean check(int a) {
        int delta = a;
        int[] nums = new int[4];
        for(int i = 0;i < 4;i++) {
            nums[i] = delta % 10;
            delta /= 10;
        }
        for(int i = 0;i < 4;i++) {
            for(int j = i + 1;j < 4;j++) {
                for(int k = j + 1;k < 4;k++) {
                    for(int l = k + 1;l < 4;l++) {
                        if((nums[i] * 10 + nums[j]) * (nums[k] * 10 + nums[l]) == a) return true;
                        else if((nums[i] * 10 + nums[j]) * (nums[l] * 10 + nums[k]) == a) return true;
                        else if((nums[i] * 10 + nums[k]) * (nums[j] * 10 + nums[l]) == a) return true;
                        else if((nums[i] * 10 + nums[k]) * (nums[l] * 10 + nums[j]) == a) return true;
                        else if((nums[i] * 10 + nums[l]) * (nums[j] * 10 + nums[k]) == a) return true;
                        else if((nums[i] * 10 + nums[l]) * (nums[k] * 10 + nums[j]) == a) return true;
                        else if((nums[j] * 10 + nums[i]) * (nums[k] * 10 + nums[l]) == a) return true;
                        else if((nums[j] * 10 + nums[i]) * (nums[l] * 10 + nums[k]) == a) return true;
                        else if((nums[j] * 10 + nums[k]) * (nums[l] * 10 + nums[i]) == a) return true;
                        else if((nums[j] * 10 + nums[k]) * (nums[i] * 10 + nums[l]) == a) return true;
                        else if((nums[j] * 10 + nums[l]) * (nums[k] * 10 + nums[i]) == a) return true;
                        else if((nums[j] * 10 + nums[l]) * (nums[i] * 10 + nums[k]) == a) return true;
                        else if((nums[k] * 10 + nums[i]) * (nums[j] * 10 + nums[l]) == a) return true;
                        else if((nums[k] * 10 + nums[i]) * (nums[l] * 10 + nums[j]) == a) return true;
                        else if((nums[k] * 10 + nums[j]) * (nums[i] * 10 + nums[l]) == a) return true;
                        else if((nums[k] * 10 + nums[j]) * (nums[l] * 10 + nums[i]) == a) return true;
                        else if((nums[k] * 10 + nums[l]) * (nums[i] * 10 + nums[j]) == a) return true;
                        else if((nums[k] * 10 + nums[l]) * (nums[j] * 10 + nums[i]) == a) return true;
                        else if((nums[l] * 10 + nums[i]) * (nums[j] * 10 + nums[k]) == a) return true;
                        else if((nums[l] * 10 + nums[i]) * (nums[k] * 10 + nums[j]) == a) return true;
                        else if((nums[l] * 10 + nums[j]) * (nums[i] * 10 + nums[k]) == a) return true;
                        else if((nums[l] * 10 + nums[j]) * (nums[k] * 10 + nums[i]) == a) return true;
                        else if((nums[l] * 10 + nums[k]) * (nums[j] * 10 + nums[i]) == a) return true;
                        else if((nums[l] * 10 + nums[k]) * (nums[i] * 10 + nums[j]) == a) return true;
                    }
                }
            }
        }
        return false;
    }
    
    public static void main(String[] args) {
        for(int i = 1000;i <= 9999;i++) {
            if(i % 100 == 0) continue;
            if(check(i)) println(i);
        }
    }
}
/* Output:
 * 1260
 * 1395
 * 1435
 * 1530
 * 1827
 * 2187
 * 6880
*/
最后修改:2019 年 05 月 25 日 06 : 09 PM

发表评论