• 中文
    • English
  • 注册
  • 查看作者
  • 《算法第四版》课后练习题1.1.1-1.1.10答案

    习题1.1.1

    给出以下表达式的值:

    a.  (0 + 15) /  2

    b.  2.0e-6 * 100000000.1

    c.  true && false || true && true

    参考答案:

    a.  7

    b.  200.0000002   (2.0e-6 是指数形式,等于2.0 * 10 ^ -6)

    c.  true


    习题1.1.2

    给出以下表达式的类型和值:

    a.  (1 + 2.236)/2

    b.  1 + 2 + 3 + 4.0

    c.  4.1 >= 4

    d.  1 + 2 + “3”

    参考答案:

    a.  1.618

    b.  10.0

    c.  true

    d.  33


    习题1.1.3 

    编写一个程序,从命令行得到三个整数参数。如果它们都相等则打印 equal,否则打印 not equal

    参考答案:

    public class Test {
        public static void main(String[] args) {
            String str = args[0].equals(args[1]) && args [1].equals(args[2]) ?" equal " :" not equal ";
            System.out.println(str);
        }
    }

    习题1.1.4 

    下列语句各有什么问题(如果有的话)?

    a. if (a > b) then c = 0;

    b. if a > b { c = 0; }

    c. if (a > b) c = 0;

    d. if (a > b) c = 0 else b = 0;

    参考答案:

    a:没有then这个关键字

    b:a > b 缺少()

    c:没有错误

    d:c = 0 后面缺少;


    习题1.1.5 

    编写一段程序,如果 double 类型的变量 x 和 y 都严格位于 0 和 1 之间则打印 true,否则打印 false。

    参考答案:

    import java.util.Scanner;
    
    public class Test {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            System.out.println("请输入x:");
            double x = input.nextDouble();
            System.out.println("请输入y:");
            double y = input.nextDouble();
            System.out.println((x > 0 && x < 1) && (y > 0 && y < 1));
        }
    }

     


    习题1.1.6 

    下面这段程序会打印出什么?

        public static void main(String[] args) {
            int f = 0;
            int g = 1;
            for (int i = 0; i <= 15; i++) {
                StdOut.println(f);
                f = f + g;
                g = f - g;
            }
        }

    参考答案:

    0

    1

    1

    2

    3

    5

    8

    13

    21

    34

    55

    89

    144

    233

    377

    610


    习题1.1.7 

    分别给出以下代码段打印出的值:

    a.

            double t = 9.0;
            while (Math.abs(t - 9.0/t) > .001)
                t = (9.0/t + t) / 2.0;
            StdOut.printf("%.5f\n", t);

    b.

            int sum = 0;
            for (int i = 1; i < 1000; i++)
                for (int j = 0; j < i; j++)
                    sum++;
            StdOut.println(sum);

    c.

    int sum = 0;
    for (int i = 1; i < 1000; i *= 2)
        for (int j = 0; j < 1000; j++)
            sum++;
    StdOut.println(sum);

    参考答案:

    a:3.00009

    b:499500

    c:10000


    习题1.1.8 

    下列语句会打印出什么结果?给出解释。

    a.

    System.out.println('b');

    b.

    System.out.println('b' + 'c');

    c.

    System.out.println((char) ('a' + 4));

    答案:

    a:b     (解释:直接打印char字符)

    b:197 (解释:98 + 99 = 197)

    c:e     (解释:97 + 4 = 101,e的ASCII码是101)


    习题1.1.9 

    编写一段代码,将一个正整数 N 用二进制表示并转换为一个 String 类型的值 s。

    参考答案:

    解法一:

    public class Test {
        public static String DECtoBIN(int n) {
            return ("" + Integer.toBinaryString(n));
        }
    
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            System.out.println("请输入一个正整数N:");
            int n = input.nextInt();
            System.out.println(DECtoBIN(n));
        }
    }

    解法二:

    public static String DECtoBIN(int n) {
        String s = "";
        for (int i = n; i > 0; i /= 2) {
            s = (i % 2) + s;
        }
        return s;
    }

    解法三:

    public static String DECtoBIN(int n) {
        String resultString = "";
        for (int i = 31; i >= 0; i--) //int类型占32位
            resultString = resultString + (n >>> i & 1);
        // &:位与:第一个操作数的的第n位于第二个操作数的第n位如果都是1,那么结果的第n为也为1,否则为0
        // >>> :无符号右移
        return resultString;
    }

    摘自Kyson博客


    习题1.1.10

    下面这段代码有什么问题?

     int[] a;
      for (int i = 0; i < 10; i++)
      a[i] = i * i;

    参考答案:

    没有为数组a分配内存

  • 0
  • 0
  • 0
  • 9.5k
  • 请登录之后再进行评论

    登录
    单栏布局 侧栏位置: