Skip to content

Latest commit

 

History

History
351 lines (222 loc) · 5.19 KB

Ex_1_1.md

File metadata and controls

351 lines (222 loc) · 5.19 KB
title date draft tags categories
算法4 Java解答 1.1
2019-01-28 06:00:00 +0800
false
JAVA
技术
归档

1.1

1.1.1

问题:

Give the value of each of the following expressions:

a. (0+15) / 2

b. 2.0e-6 * 100000000.1

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

分析:

a. 7

b. 200.0000002

c. true

  public static void main(String[] args) {

    // 转换为int
    StdOut.println((0 + 15) / 2);

    // double 类型的运算
    StdOut.println(2.0e-6 * 100000000.1);

    // && 的优先级 大于 ||
    StdOut.println(true && false || true && true);

//    7
//    200.0000002
//    true
  }

1.1.2

问题:

1.1.2 Give the type and value of each of the following expressions:

a. (1 + 2.236)/2

b. 1 + 2 + 3 + 4.0

c. 4.1 >= 4

d. 1 + 2 + "3"

分析:

  public static void main(String[] args) {

    StdOut.println((1 + 2.236) / 2);

    StdOut.println(1 + 2 + 3 + 4.0);

    StdOut.println(4.1 >= 4);

    StdOut.println(1+2+"3");

//    double 1.618
//    double 10.0
//    boolean true
//    true 33

  }

1.1.3

问题:

1.1.3 Write a program that takes three integer command-line arguments and prints equal if all three are equal, and not equal otherwise.

分析:

  public static void main(String[] args) {

    int num1 = StdIn.readInt();
    int num2 = StdIn.readInt();
    int num3 = StdIn.readInt();

    if (num1 == num2 && num2 == num3)
      StdOut.println("equal");
    else
      StdOut.println("not equal");

  }

1.1.4

问题:

1.1.4 What (if anything) is wrong with each of the following statements?

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;

分析:

  public static void main(String[] args) {
    int a = 0;
    int b = 2;
    int c = 100;

    // a.Java 中没有 then 关键字
//    if(a>b) then c = 0 ;

    // b. 没有括号
//    if a > b { c = 0;}

    // 正确
    if (a > b) c = 0;

    // 没有 ;
//    if (a > b) c = 0 else b = 0;
  }

1.1.5

问题:

1.1.5 Write a code fragment that prints true if the double variables x and y are both strictly between 0 and 1 and false otherwise.

分析:

  public static void main(String[] args) {
//    1.1.5 Write a code fragment that prints true if the double variables x and y are both strictly between 0 and 1 and false otherwise.

    double num1 = StdIn.readDouble();
    double num2 = StdIn.readDouble();

    if( (num1>0 && num1 <1) && (num2>0 && num2 <1) )
      StdOut.println(true);
    else
      StdOut.println(false);

//    0
//    1
//    false

//    0.99999
//    0.00001
//    true
  }

1.1.6

问题:

1.1.6 What does the following program print?

分析:

  public static void main(String[] args) {
//    1.1.6 What does the following program print?

    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

问题:

1.1.7 Give the value printed by each of the following code fragments:

分析:

  @Test
  public void testA() {
    double t = 9.0;
    while (Math.abs(t - 9.0 / t) > .001)
      t = (9.0 / t + t) / 2.0;
    StdOut.printf("%.5f\n", t);
  }

  @Test
  public void testB(){
    int sum = 0;
    for (int i = 1; i < 1000; i++) {
      for (int j = 0; j < i; j++) {
        sum++;
      }
    }
    StdOut.println(sum);
  }

  @Test
  public void testC(){
    int sum = 0;
    for (int i = 1; i < 1000; i*=2) {
      for (int j = 0; j < i; j++) {
        sum++;
      }
    }
    StdOut.println(sum);
  }

1.1.8

问题:

1.1.8 What do each of the following print?

a. System.out.println('b');

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

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

Explain each outcome.

分析:

public class Ex_1_08 {

  public static void main(String[] args) {
//    1.1.8 What do each of the following print?
// a. System.out.println('b');
// b. System.out.println('b' + 'c');
// c. System.out.println((char) ('a' + 4));
//    Explain each outcome.

    // char to String
    System.out.println('b');

    // b char 类型 转换为 98, c 转换为99
    System.out.println('b'+'c');

    // e : 'a' char to int , 97, 97+4=111, 111 to char = e
    System.out.println((char)('a'+4));
  }
}

1.1.9

问题:

1.1.9 Write a code fragment that puts the binary representation of a positive integer N into a String s. Solution: Java has a built-in method Integer.toBinaryString(N) for this job, but the point of the exercise is to see how such a method might be implemented. Here is a particularly concise solution:

String s = "";
for (int n = N; n > 0; n /= 2) 
  s = (n % 2) + s;

分析:

1.1.10

问题:

1.1.10 What is wrong with the following code fragment?

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

分析:

It does not allocate memory for a[] with new. This code results in a variable a might not have been initialized compile-time error.