算法练习(1):Java基本语法(1.1.1-1.1.7)

本系列博客习题来自《算法(第四版)》,算是本人的读书笔记,如果有人在读这本书的,欢迎大家多多交流。为了方便讨论,本人新建了一个微信群(算法交流),想要加入的,请添加我的微信号:zhujinhui207407 谢谢。另外,本人的个人博客 http://www.kyson.cn 也在不停的更新中,欢迎一起讨论
算法(第4版)

知识点

  • java的基本语法
  • 斐波那契数列的java表示

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

public class Practice1 {
    public static void main(String []args) {
        System.out.println((0 + 15)/2);
        System.out.println(2.0e-6 * 100000000.1);
        System.out.println(true && false || true && true);
    }
}

答案

7   //类型是整形,所以输出7
200.0000002    //浮点型
true    //false||true,输出

1.1.2 给出以下表达式的类型和值:
a. (1 + 2.236)/2
b. 1 + 2 + 3 + 4.0
c. 4.1 >= 4
d. 1 + 2 + “3”

public class Practice2 {
    public static void main(String []args) {
        System.out.println((1 + 2.236)/2);
        System.out.println(1 + 2 + 3 + 4.0);
        System.out.println(4.1 >= 4);
        System.out.println(1 + 2 + "3");
    }
}

答案

1.618 //浮点型
10.0   //1,2,3会转换为浮点型
true   //真
33     //数字转化为字符串

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

答案

import java.util.Scanner;
public class Practice3 {  
    public static void main(String[] args) {  
        System.out.println("请输入三个整数");
        Scanner scanner1 = new Scanner(System.in); 
        String string1 = scanner1.next();

        Scanner scanner2 = new Scanner(System.in); 
        String string2 = scanner2.next();

        Scanner scanner3 = new Scanner(System.in); 
        String string3 = scanner3.next();

        Integer number1 = Integer.valueOf(string1);
        Integer number2 = Integer.valueOf(string2);
        Integer number3 = Integer.valueOf(string3);

        if(number1 == number2 && number1 == number3 && number2 == number3) {
             System.out.println("equal");
        } else {
             System.out.println("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

答案

       public boolean between0And1(x,y){
           if((x < 1) && (x > 0) && (y < 1) && (y > 0)) {
               return true;
           }
           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);

答案

3.00009
499500
10000
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值