本系列博客的习题都来自《算法(第四版)》,如果有其他人也在看这本书,欢迎来评论区多多交流
1.1.1 给出以下表达式的值:
a. ( 0 + 15 ) / 2
b. 2.0e-6 * 100000000.1
c. true && false || true && true
代码
@Test
fun test1_1_1(){
println((0 + 15) / 2)
println(2.0e-6 * 100000000.1)
println(true && false || true && true)
}
答案
7 //int类型数据,所以输出7
200.0000002 //浮点型
true //true && false || true && true -> false || true && true -> true && true -> true
1.1.2 给出以下表达式的类型和值:
a. (1 + 2.236)/2
b. 1 + 2 + 3 + 4.0
c. 4.1 >= 4
d. 1 + 2 + "3"
代码
@Test
fun test1_1_2(){
println((1 + 2.236) / 2)
println(1 + 2 + 3 + 4.0)
println(4.1 >= 4)
test_1_1_2_()
}
public void test_1_1_2_() {
System.out.println(1 + 2 + "3");
}
答案
1.618 //浮点型 10.0 //由于4.0是浮点数,所以结果转为浮点型 true //真 33 //数字转成了字符串 1 + 2 + "3" -> 3+"3" -> "33"
1.1.3 编写一个程序,从命令行得到三个整数参数。如果它们都相等则打印 equal,否则打印 not equal
代码
@JvmStatic
fun main(args: Array) {
test1_1_3()
}
//1.1.3 编写一个程序,从命令行得到三个整数参数。如果它们都相等则打印 equal,否则打印 not equal。
private fun test1_1_3() {
println("请输入三个整数参数")
val scanner1 = Scanner(System.`in`)
val int1 = scanner1.nextInt()
val scanner2 = Scanner(System.`in`)
val int2 = scanner2.nextInt()
val scanner3 = Scanner(System.`in`)
val int3 = scanner3.nextInt()
if (int1 == int2 && int1 == int3) {
println("equal")
} else {
println("not equal")
}
}
答案
请输入三个整数参数 5 5 5 equal
请输入三个整数参数 5 6 4 not equal
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 then关键字有问题,java中没有then关键字
b a > b 忘记加括号了
c 正确
d c = 0 忘记加分号,并且没有花括号,b = 0;外没有加花括号
1.1.5 编写一段程序,如果 double 类型的变量 x 和 y 都严格位于 0 和 1 之间则打印 true,否则打印 false
答案
@Test
fun test1_1_5(x: Double, y: Double) {
if (x > 0 && x < 1 && y > 0 && y < 1) {
println(true)
} else {
println(false)
}
}
1.1.6 下面这段程序会打印出什么?
@Test
fun test1_1_6() {
var f = 0
var g = 1
for (i in 0..15) {
println(f)
f = f + g
g = f - g
}
}
答案
15个循环过程中,值的变化
//f = 0;g = 1;
//f = 1;g = 0;
//f = 1;g = 1;
//f = 2;g = 1;
//f = 3;g = 2;
//f = 5;g = 3;
//f = 8;g = 5;
//f = 13;g = 8;
//f = 21;g = 13;
//f = 34;g = 21;
//f = 55;g = 34;
//f = 89;g = 55;
//f = 144;g = 89;
//f = 233;g = 144;
//f = 377;g = 233;
//f = 610;g = 377;
打印的是f的值
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("%.5fn", 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);
答案
@Test
fun test1_1_7() {
test1_1_7_1()
test1_1_7_2()
test1_1_7_3()
}
//t=9
//t=5
//t=3.4
//t=3.02352941
//t=3.02352941
//t=3.00009
private fun test1_1_7_1() {
var t = 9.0
while (Math.abs(t - 9.0 / t) > .001) t = (9.0 / t + t) / 2.0
System.out.printf("%.5fn", t)
}
//sum=1+2+3+4+....+998+999
//sum=(1+999)+(2+998)+....+(499+501)+500
//sum=1000*499+500=499500
private fun test1_1_7_2() {
var sum = 0
for (i in 1..999) for (j in 0 until i) sum++
println(sum)
}
//i=1,2,4,8,....512,1024 --->即从 2的0次方 到 2的9次方,一共while循环了10次
//sum=1000*10=10000
private fun test1_1_7_3() {
var sum = 0
var i = 1
while (i < 1000) {
for (j in 0..999) sum++
i *= 2
}
println(sum)
}
3.00009 499500 10000



