算法第四版习题答案

上传人:人*** 文档编号:486225189 上传时间:2023-03-31 格式:DOCX 页数:29 大小:175.75KB
返回 下载 相关 举报
算法第四版习题答案_第1页
第1页 / 共29页
算法第四版习题答案_第2页
第2页 / 共29页
算法第四版习题答案_第3页
第3页 / 共29页
算法第四版习题答案_第4页
第4页 / 共29页
算法第四版习题答案_第5页
第5页 / 共29页
点击查看更多>>
资源描述

《算法第四版习题答案》由会员分享,可在线阅读,更多相关《算法第四版习题答案(29页珍藏版)》请在金锄头文库上搜索。

1、1.1.1 给出以下表达式的值:a. ( 0 + 15 )/ 2b. 2.0e-6 * 100000000.1c. true & false | true & true 答案:a.7,b.200.0000002 c.ture1.1.2 给出以下表达式的类型和值:a. (1 + 2.236)/2b. 1 + 2 + 3 + 4.0c. 4.1 = 4d. 1 + 2 + 3答案:a.1.618 b. 10.0 c.true d.331.1.3 编写一个程序,从命令行得到三个整数参数。如果它们都相等则打印equal,否贝U打印 not equal 。public class TestUqualpu

2、blic static void main( Stri ng args) int a,b,c;a=b=c=0;(Please enter three numbersStdOut. println a =Stdl (););n.read Int ();b=Stdl n.readI nt();c=StdI n.read Intif (equals (a,b,c)=1) StdOut. print (equal); else StdOut. print (not equal);public static int equals( int a , int b , int c) if (a=b&b=c)r

3、eturn 1; else return 0; 1.1.4 下列语句各有什么问题(如果有的话)?a. if (a b) the n 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( Stri ng args)

4、 double x;double y;x=Stdl n.readDouble();y=Stdl n.readDouble();StdOut. print ( compare(x)& compare(y);public static boolea n compare(double x)If(x0& x1)reture n ture;elsereturn false;1.1.6 下面这段程序会打印出什么?int f = 0;int g = 1;for (int i = 0; i .001)t = (9.0/t + t) / 2.0;StdOut.pri ntf(%.5fn, t);b. int s

5、um = 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 0; n /= 2)s = (n % 2) + s;1.1.10 下面这段代码有什么问题?int a;for (int i = 0; i 10; i+)ai = i * i;解答:它没有用 new 为 a 分配内存。这段代码会产生一个 variable a might not have been ini

6、tialized 的编译错误。1.1.11 编写一段代码,打印出一个二维布尔数组的内容。其中,使用 * 表示真,空格表示假。打印出行号和列号。public class Test public Test() / TODOAuto-generated constructor stub public static void main(String args) / TODOAuto-generated method stubboolean a = new boolean 1010;a=RandomInitial(a);/ 随机初始化 打TestPrint (a);/印数组public static v

7、oid TestPrint( boolean a) for ( int i=0;ia.length ;i+)打印行号publicStdOut. print ( +i);StdOut. println ( ); for (int i=0;i10;i+) StdOut. print (i); for ( int j=0;j10;j+) if (aij)StdOut. print (* + ); elseStdOut. print ( +); StdOut. println ();static boolean RandomInitial(boolean a)for ( int i=0;ia. len

8、gth ;i+)for ( int j=0;ja. length ;j+)if (StdRandom. bernoulli (0.1)a皿二trueelseaij= falsereturn a;1.1.12 以下代码段会打印出什么结果?int口 a = new int10;for (int i = 0; i 10; i+)ai = 9 - i;for (int i = 0; i 10; i+)ai = aai;for (int i = 0; i 10; i+) System.out.println(i);答案:0 1 2 3 4 5 6 7 8 9如 System.out.pri ntl n(

9、 ai);0 1 2 3 4 4 3 2 1 01.1.13 编写一段代码,打印出一个M行N列的二维数组的转置(交换行和列)public class Migrate public Migrate() / TODOAuto-generated constructor stubpublic static void main(String口 args) / TODOAuto-generated method stub int m=5; int n=5;int a=new int mn;int b=new int nm;a= RandomInitial (a,n);/ 初始化二维薮组b= Migrat

10、eArrays (a,b);/ 转置二维数组MigratePrint (b); 输出转置二维数组public static void MigratePrint( int a) length ;i+)length ;j+) (aij+);StdOut. println (输出转置二维数组 :”);for ( int i=0;ia. for ( int j=0;ja0. StdOut. printStdOut. println ();public static int MigrateArrays( int a, int b)for ( int i=0;ia. length ;i+) for ( i

11、nt j=0;ja0. length ;j+) b皿i=a皿;return b;public static int RandomInitial( int a, int N) StdOut. println (初始化二维数组 :”);for ( int i=0;ia. length ;i+) for (int j=0;j=M)N=N/M; a+; return a;1.1.15 编写一个静态方法histogram(),接受一个整型数组a和一个整数 M为参数弁a返回一个大小为M的数组,其中第i个元素的值为整数i在参数数组中出现的次数。如果中的值均在0到M-1之间,返回数组中所有元素之和应该和a.length 相等。public static int 口 histogram( int a, int M)int b= new int M;int n=0;int m=0;for ( int i=0;iM;i+) for ( int j=0;ja. length ;j+) if (i=aj) n+;bi=n;n=0;for ( int i=0;iM;i+) m=m+b

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 商业/管理/HR > 营销创新

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号