- 1、本文档共25页,可阅读全部内容。
- 2、有哪些信誉好的足球投注网站(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
算法第四版习题答案
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 + 3
答案: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(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==bb==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(x0x1)
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(%.5f\n, t);
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 有一
文档评论(0)