1.1.1
package ch1.ch1_1.pratice; public class P1_1_1{ public static void main(String[] args) { double aa = (0+15)/2; int a = (0 + 15) / 2; double b = Math.pow(2.0, -6) * 100000000.1; //Math.Pow(double x, double y) 求x的y次方 boolean c = true && false || true && true; System.out.println(a+" "+aa+" "+b); if (c) System.out.println(true); else System.out.println(false); } }
1.1.2
package ch1.ch1_1.pratice;
import edu.princeton.cs.algs4.StdOut;
public class P1_1_2 {
public static void main(String[] args) {
double a = (1+2.236)/2;
double b = 1+2+3+4.0;
boolean c= 4.1<=4;
String d = 1+2+"3";
System.out.println(a+" "+b+" "+d);
if(c) StdOut.println(true);
else StdOut.println(false);
}
}
1.1.3
package ch1.ch1_1.pratice;
import java.util.Scanner;
import edu.princeton.cs.algs4.StdIn;
public class P1_1_3 {
public static void main(String[] args) {
int a = StdIn.readInt();//stdIn 中的输入是从控制台输入
int b = StdIn.readInt();//相当于int b = scanner.nextInt();
int c = StdIn.readInt();
if (a == b && a == c) {
System.out.println("equal");
} else {
System.out.println("not equal");
}
}
}
1.1.5
package ch1.ch1_1.pratice;
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;
public class P1_1_5 {
public static void main(String[] args) {
double x = StdIn.readDouble();
if (x>0.0&&x<1.0) StdOut.println("true");
else StdOut.println("false");
}
}
1.1.6
package ch1.ch1_1.pratice;
import edu.princeton.cs.algs4.StdOut;
public class P1_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
package ch1.ch1_1.pratice;
import edu.princeton.cs.algs4.StdOut;
public class P1_1_7a {
public static void main(String[] args) {
double t = 9.0;
while (Math.abs(t - 9.0 / t) > .001) {
t = (9.0 / t + t) / 2.0;
}
StdOut.printf("%.5f\n", t);
}
}
/*
* 3.00009
* */
package ch1.ch1_1.pratice; public class P1_1_7b { public static void main(String[] args) { int sum = 0; for (int i = 1; i < 1000; i++) for (int j = 0; j < i; j++) sum++; System.out.println(sum);//499500 } }
1.1.7c
package ch1.ch1_1.pratice;
public class P1_1_7c {
public static void main(String[] args) {
int sum = 0;
for(int i = 1;i<1000;i*=2){
for(int j = 0;j<1000;j++)
{sum++;}
}
System.out.println(sum);//10000
}
}
1.1.8
package ch1.ch1_1.pratice;
public class P1_1_8 {
public static void main(String[] args) {
System.out.println('a');//a
System.out.println('b'+'c');//98+99
System.out.println((char)('a'+4));//e
}
}