算法第四版习题解答(1.1 Basic Programming Model)
前言
使用的是《算法》第四版英文版,主要是习题解答。
书中jar包的导入请戳:算法(第四版)中algs4.jar下载和使用
EXERCISES
1.1.1
1.1.2
1.1.3
import java.util.Scanner;
public class Something {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = scanner.nextInt();
if (a == b && a == c)
System.out.println("equal");
else
System.out.println("not equal");
}
}
1.1.4
a. 去掉then
b. a > b用括号括起来
c. true
d. true
1.1.5
import java.util.Scanner;
public class Something {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double x = scanner.nextDouble();
double y = scanner.nextDouble();
if ((x > 0.0 && x < 1.0) && (y > 0.0 && y < 1.0))
System.out.println("true");
else
System.out.println("false");
}
}
1.1.6
1.1.7
1.1.8
1.1.9
import java.util.Scanner;
// 不建议用String,因为对String对象的修改要重新创建一个String对象
// 可以好好查查资料,String、StringBuild和StringBuffer的区别
public class Something {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
StringBuilder sb = new StringBuilder();
while (N > 0) {
int temp = N % 2;
N /= 2;
sb.append(temp);
}
sb.reverse();
System.out.println(sb);
}
}
1.1.10
// 修正如下。主要是未给数组分配内存空间
int[] a = new int[10];
1.1.11
import java.util.Scanner;
public class Something {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int row = scanner.nextInt(); // 二维数组的行数
int col = scanner.nextInt(); // 二维数组的列数
boolean[][] res = new boolean[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
res[i][j] = scanner.nextBoolean();
}
}
// ============之间的为二维数组打印
System.out.println("=============");
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (res[i][j] == true)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
System.out.println("=============");
}
}
1.1.12
1.1.13
import java.util.Scanner;
public class Something {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int M = scanner.nextInt(); // 二维数组的行数
int N = scanner.nextInt(); // 二维数组的列数
int[][] res = new int[M][N];
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
res[i][j] = scanner.nextInt();
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
System.out.print(res[j][i] + " ");
}
System.out.println();
}
}
}
1.1.14
public static int lg(int N) {
int res = -1;
if (N > 0) {
N /= 2;
res++;
}
return res;
}
1.1.15
public static int[] histogram(int[] a, int M) {
int[] res = new int[M];
// 返回数组初始化
for (int i = 0; i < M; i++) {
res[i] = 0;
}
for (int i = 0; i < M; i++) {
for (int j = 0; j < a.length; j++) {
if (a[j] == i)
res[i]++;
}
}
return res;
}
1.1.16
1.1.17
1.1.18
1.1.19
我一定是时间太多了,才会运行这个程序。
1.1.20
public static double F(int N) {
if (N == 0)
return 0;
if (N == 1)
return 0;
return F(N - 1) + Math.log10((double) N);
}
1.1.21
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt(); // 输入的数据的行数
String[] name = new String[N]; // 存储名字
int[] num1 = new int[N]; // 存储第一个数字
int[] num2 = new int[N]; // 存储第二个数字
double[] num3 = new double[N]; // 存储计算的数字
for (int i = 0; i < N; i++) {
name[i] = scanner.next();
num1[i] = scanner.nextInt();
num2[i] = scanner.nextInt();
num3[i] = (double) num2[i] / num1[i];
}
System.out.println("==============="); // 打印表格
for (int i = 0; i < N; i++) {
System.out.println(name[i] + " " + num1[i] + " " + num2[i] + " " + num3[i]);
}
System.out.println("===============");
}
}
1.1.22
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt(); // 数组长度
int[] a = new int[N];
for (int i = 0; i < N; i++) {
a[i] = scanner.nextInt();
}
int key = scanner.nextInt();
rank(key, a);
}
public static int rank(int key, int[] a) {
return rank(key, a, 0, a.length - 1, 0);
}
public static int rank(int key, int[] a, int lo, int hi, int depth) {
System.out.println(depth + ": " + lo + " " + hi);
if (lo > hi)
return -1;
int mid = lo + (hi - lo) / 2;
if (key < a[mid]) {
return rank(key, a, lo, mid - 1, ++depth);
}
else if (key > a[mid]) {
return rank(key, a, mid + 1, hi, ++depth);
}
else
return mid;
}
}
1.1.23
题目没读懂,尴尬~
1.1.24
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int p = scanner.nextInt();
int q = scanner.nextInt();
int res = gcd(p, q);
System.out.println(res);
}
public static int gcd(int p, int q) {
System.out.println(p + " " + q);
if (q == 0)
return p;
int r = p % q;
return gcd(q, r);
}
}
1.1.25
该题证明请戳➡:欧几里得算法证明
CREATIVE PROBLEMS
1.1.26
1.1.27
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
int k = scanner.nextInt();
double p = scanner.nextDouble();
System.out.println(binomial(N, k, p));
}
public static double binomial(int N, int k, double p) {
if (N == 0 && k == 0)
return 1.0;
if (N < 0 || k < 0)
return 0.0;
return (1.0 - p) * binomial(N - 1, k, p) + p * binomial(N - 1, k - 1, p);
}
}
1.1.28
略
1.1.29
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt(); // 数组长度
int[] a = new int[N];
for (int i = 0; i < N; i++) {
a[i] = scanner.nextInt();
}
int key = scanner.nextInt();
System.out.println(rank(key, a, 0, a.length - 1));
System.out.println(count(key, a, 0, a.length - 1, 1) -
count(key, a, 0, a.length - 1, 0) + 1);
}
public static int rank(int key, int[] a, int lo, int hi) {
if (lo > hi)
return -1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (a[mid] >= key)
hi = mid - 1;
else
lo = mid + 1;
}
return lo;
}
public static int count(int key, int[] a, int lo, int hi, int flag) {
int res = -1;
if (lo > hi)
return -1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (a[mid] > key)
hi = mid - 1;
else if (a[mid] < key)
lo = mid + 1;
else {
res = mid;
if (flag == 0) // 找左边界
hi = mid - 1;
else // 找右边界
lo = mid + 1;
}
}
return res;
}
}
1.1.30
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int M = scanner.nextInt(); // 数组行数
int N = scanner.nextInt(); // 数组列数
boolean[][] a = new boolean[M][N];
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
if (gcd(i, j) == 1)
a[i][j] = true;
else
a[i][j] = false;
}
}
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
public static int gcd(int p, int q) {
if (q == 0)
return p;
int r = p % q;
return gcd(q, r);
}
}
1.1.31
该题题解请戳➡:题解
1.1.32
该题题解请戳➡:题解
1.1.33
public class Matrix {
public static double dot(double[] x, double[] y) {
if (x.length != y.length)
return -1.0;
double res = 0.0;
for (int i = 0; i < x.length; i++) {
res += x[i] * y[i];
}
return res;
}
public static double[][] mult(double[][] a, double[][] b) {
if (a[0].length != b.length)
return new double[0][0];
int row = a.length;
int col = b[0].length;
double[][] res = new double[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
res[i][j] = 0;
for (int k = 0; k < b.length; k++) {
res[i][j] += a[i][k] * b[k][j];
}
}
}
return res;
}
public static double[][] transpose(double[][] a) {
int row = a.length;
int col = a[0].length;
double[][] res = new double[col][row];
for (int i = 0; i < col; i++) {
for (int j = 0; j < row; j++) {
res[i][j] = a[j][i];
}
}
return res;
}
public static double[] mult(double[][] a, double[] x) {
if (a[0].length != x.length)
return new double[0];
int len = a.length;
double[] res = new double[len];
for (int i = 0; i < len; i++) {
double sum = 0.0;
for (int j = 0; j < x.length; j++) {
sum += a[i][j] * x[j];
}
res[i] = sum;
}
return res;
}
public static double[] mult(double[] y, double[][] a) {
if (y.length != a.length)
return new double[0];
int len = a[0].length;
double[] res = new double[len];
for (int i = 0; i < len; i++) {
double sum = 0.0;
for (int j = 0; j < y.length; j++) {
sum += y[j] * a[j][i];
}
res[i] = sum;
}
return res;
}
}
1.1.34
a. 无需保存,可以实现为过滤器
b. 需要保存
c. 无需保存,可以实现为过滤器
d. 无需保存,可以实现为过滤器
e. 无需保存,可以实现为过滤器
f. 需要保存
g. 需要保存
h. 无需保存