算法 第四版 习题 答案

2020-06-21 14:54

1.1.1 给出以下表达式的值: a. ( 0 + 15 ) / 2 b. 2.0e-6 * 100000000.1

c. true && false || true && true 答案:a.7,b.200.0000002 c.ture 1.1.2 给出以下表达式的类型和值: a. (1 + 2.236)/2 b. 1 + 2 + 3 + 4.0 c. 4.1 >= 4 d. 1 + 2 + \

答案:a.1.618 b. 10.0 c.true d.33

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

public class TestUqual { public static void main(String[] args) { int a,b,c; a=b=c=0; StdOut.println(\); a =StdIn.readInt(); b=StdIn.readInt(); c=StdIn.readInt(); if(equals(a,b,c)==1) { StdOut.print(\); } else { StdOut.print(\); } } public static int equals(int a ,int b , int c) { if(a==b&&b==c) { return 1; } else { return 0; } } }

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. if (a > b) c = 0; b. if (a > b) { c = 0; }

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

public class TestUqual { public static void main(String[] args) {

double x; double y;

x=StdIn.readDouble();

y=StdIn.readDouble();

StdOut.print(compare(x)&& compare(y)); }

public static boolean compare(double x)

{

If(x>0&&x<1) returen ture; else return false; } }

1.1.6 下面这段程序会打印出什么? 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(\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 b. 197 c. e

1.1.9 编写一段代码,将一个正整数N 用二进制表示并转换为一个String 类型的值s。 解答:Java 有一个内置方法Integer.toBinaryString(N) 专门完成这个任务,但该题的目的就是给出这个方法的其他实现方法。 下面就是一个特别简洁的答案: String s = \

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

1.1.10 下面这段代码有什么问题? int[] a;

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

解答:它没有用new 为a[] 分配内存。这段代码会产生一个variable a might not have been initialized 的编译错误。

1.1.11 编写一段代码,打印出一个二维布尔数组的内容。其中,使用* 表示真,空格表示假。打印出行号和列号。

public class Test { public Test() { // TODO Auto-generated constructor stub } public static void main(String[] args) { // TODO Auto-generated method stub boolean[][] a = new boolean[10][10]; a=RandomInitial(a);//随机初始化 TestPrint(a);//打印数组 }

}

public static void TestPrint(boolean [][] a) { for(int i=0;i

public static boolean[][] RandomInitial( boolean [][] a) { for(int i=0;i

1.1.12 以下代码段会打印出什么结果? int[] a = new int[10]; for (int i = 0; i < 10; i++) a[i] = 9 - i;

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

for (int i = 0; i < 10; i++) System.out.println(i); 答案:0 1 2 3 4 5 6 7 8 9 如System.out.println(a[i]); 0 1 2 3 4 4 3 2 1 0

1.1.13 编写一段代码,打印出一个M 行N 列的二维数组的转置(交换行和列)。

public class Migrate { public Migrate() { // TODO Auto-generated constructor stub }

public static void main(String[] args) { // TODO Auto-generated method stub int m=5; int n=5;

int [][] a=new int [m][n]; int [][] b=new int [n][m];

a=RandomInitial(a,n); //初始化二维数组 b=MigrateArrays(a,b); //转置二维数组 MigratePrint(b);//输出转置二维数组 } public static void MigratePrint(int [][] a) { StdOut.println(\输出转置二维数组:\); for (int i=0;i

for(int j=0;j

StdOut.println(); } } public static int[][] MigrateArrays(int [][] a,int [][] b) { for (int i=0;i

1.1.14 编写一个静态方法lg(),接受一个整型参数N,返回不大于log2N 的最大整数。不要使用Math 库。

public static int lga(int N,int M) { int a=0; while(N>=M)


算法 第四版 习题 答案.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:人力资源和社会保障业务考试知识

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: