ImageVerifierCode 换一换
格式:DOCX , 页数:39 ,大小:29.89KB ,
资源ID:6226448      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/6226448.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(算法 第四版 习题 答案.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

算法 第四版 习题 答案.docx

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,否则打印not equal。public class Te

2、stUqual public static void main(String args) int a,b,c; a=b=c=0; StdOut.println(Please enter three numbers); a =StdIn.readInt(); b=StdIn.readInt(); c=StdIn.readInt(); if(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) ret

3、urn 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) doubl

4、e x;double y;x=StdIn.readDouble();y=StdIn.readDouble();StdOut.print(compare(x)& compare(y);public static boolean compare(double x)If(x0&x1)returen ture;else return false;1.1.6 下面这段程序会打印出什么?int f = 0;int g = 1;for (int i = 0; i .001)t = (9.0/t + t) / 2.0;StdOut.printf(%.5fn, t);b. int sum = 0;for (in

5、t 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 havebeen initialized 的编译错误。1.1.

6、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 boolean1010; a=RandomInitial(a);/随机初始化 TestPrint(a);/打印数组 public static void TestPrint(bool

7、ean a) for(int i=0;ia.length;i+)/打印行号 StdOut.print( +i); StdOut.println( ); for(int i=0;i10;i+) StdOut.print(i); for(int j=0;j10;j+) if(aij) StdOut.print(*+ ); else StdOut.print( + ); StdOut.println( ); public static boolean RandomInitial( boolean a) for(int i=0;ia.length;i+) for(int j=0;ja.length;j

8、+) if(StdRandom.bernoulli(0.1) aij=true; else aij=false; return 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.println(ai);0 1 2 3 4 4 3 2 1 01.1.13 编写一段代

9、码,打印出一个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 mn; int b=new int nm; a=RandomInitial(a,n); /初始化二维数组 b=MigrateArrays(a,b); /转置二维数组 MigrateP

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

11、c 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 为参数并返回一个大小为M的数组,其中第i个元素的值为整数i在参数数组中出现的次数。如果a中的值均在0到M-1之间,返回数组中所有元素之和应该和a.length 相等。public static int histogram(int a,int M) i

12、nt 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+bi; return b; 1.1.16 给出exR1(6) 的返回值:public static String exR1(int n)if (n = 0) return ;return exR1(n-3) + n + exR1(n-2) + n;答案:3113611422461.1.17 找出以下递归函数的问题:public static S

13、tring exR2(int n)String s = exR2(n-3) + n + exR2(n-2) + n;if (n = 0) return ;return s;答:这段代码中的基础情况永远不会被访问。调用exR2(3) 会产生调用exR2(0)、exR2(-3) 和exR2(-6),循环往复直到发生StackOverflowError。可以修改为:public static String exR2(int n)if (n = 0) return ;String s = exR2(n-3) + n + exR2(n-2) + n;return s;1.1.18 请看以下递归函数:pu

14、blic static int mystery(int a, int b)if (b = 0) return 0;if (b % 2 = 0) return mystery(a+a, b/2);return mystery(a+a, b/2) + a;mystery(2, 25) 和mystery(3, 11) 的返回值是多少?给定正整数a 和b,mystery(a,b)计算的结果是什么?将代码中的+ 替换为* 并将return 0 改为return 1,然后回答相同的问题。答案:50,33. 225 3111.1.19 在计算机上运行以下程序:public class Fibonaccipu

15、blic static long F(int N)if (N = 0) return 0;if (N = 1) return 1;return F(N-1) + F(N-2);public static void main(String args)for (int N = 0; N 100; N+)StdOut.println(N + + F(N);计算机用这段程序在一个小时之内能够得到F(N) 结果的最大N 值是多少?开发F(N) 的一个更好的实现,用数组保存已经计算过的值。public class Fibonaccipublic static long F(int N)if (N = 0)

16、 return 0;if (N = 1) return 1;return F(N-1) + F(N-2);public static void main(String args)int a=new int 100;a=A(a);public static long A(int a) a0=0; a1=1; for (int N = 2; N 1) return Math.ln(N)+factorialln(N-1); else return 0; 1.1.21 编写一段程序,从标准输入按行读取数据,其中每行都包含一个名字和两个整数。然后用printf() 打印一张表格,每行的若干列数据包括名字

17、、两个整数和第一个整数除以第二个整数的结果,精确到小数点后三位。可以用这种程序将棒球球手的击球命中率或者学生的考试分数制成表格。public class ScoreTable public static void main(String args) String s = Lets go for lunch!; In in = new In(Se); String whitelist = in.readAllStrings();/将文件中的字符串读取到数组中 for(int i=0;iwhitelist.length;i=i+3) StdOut.print(whitelisti+ +whitel

18、isti+1+ +whitelisti+2+ ); double m=Double.parseDouble(whitelisti+1); double n=Double.parseDouble(whitelisti+2); StdOut.printf(0.3%,m/n); StdOut.println( ); 1.1.22 使用1.1.6.4 节中的rank() 递归方法重新实现BinarySearch 并跟踪该方法的调用。每当该方法被调用时,打印出它的参数lo 和hi 并按照递归的深度缩进。提示:为递归方法添加一个参数来保存递归的深度。1.1.23 为BinarySearch 的测试用例添加

19、一个参数:+ 打印出标准输入中不在白名单上的值;-,则打印出标准输入中在白名单上的值。public static int rank(int key, int a,char c) int lo = 0; int hi = a.length - 1; if(c=+) while (lo = hi) / Key is in alo.hi or not present. int mid = lo + (hi - lo) / 2; if (key amid) lo = mid + 1; else return mid; return -1; if(c=-) while (lo = hi) / Key i

20、s in alo.hi or not present. int mid = lo + (hi - lo) / 2; if (key amid) lo = mid + 1; else return -1; return 0; else return -1; 1.1.24 给出使用欧几里德算法计算105 和24 的最大公约数的过程中得到的一系列p 和q 的值。扩展该算法中的代码得到一个程序Euclid,从命令行接受两个参数,计算它们的最大公约数并打印出每次调用递归方法时的两个参数。使用你的程序计算1 111 111 和1 234 567 的最大公约数。public static int Commo

21、mDivisor(int x,int y) if(x=1|y=1) StdOut.println(x=+x+y=+y); return 1; if(x b) t = a; a = b; b = t; if (a c) t = a; a = c; c = t; if (b c) t = b; b = c; c = t; 1.1.27 二项分布。估计用以下代码计算binomial(100, 50) 将会产生的递归调用次数:public static double binomial(int N, int k, double p)if (N = 0 & k = 0) return 1.0; and i

22、f (N 0 | k 0) return 0.0;return (1.0 - p)*binomial(N-1, k, p) + p*binomial(N-1, k-1);将已经计算过的值保存在数组中并给出一个更好的实现。估计递归调用次数: 100!public static double binomial(int N,int k,double p) cnt+; StdOut.println(N=+N+k=+k+p=+p); if (N = 0 & k = 0) StdOut.println(N = 0 & k = 0); return 1.0; if (N 0 | k 0) StdOut.println(N 0 | k 0); return 0; return (1.0 - p)*binomial(N-1, k, p) + p*binomial(N-1, k-1,p); 值保存在数组中的实现方法:public static void binomialArrays(int N,int K,double p) double a=new doubleN+1K+1; a00=1; for(int j=1;jN+1;j+) aj0=aj-10*(1-p); for (int i=0;iN+1;i+) for(

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1