牛牛的快递

2024-10-09 15:56:49 浏览数 (1)

1.题目:

解析:

(1)这里主要是处理细节:超过1千克向上取整,怎么处理;

这是原理:

代码:

代码语言:javascript复制
public static void main(String[] args) {
       
        Scanner in = new Scanner(System.in);
        double a = in.nextDouble();
        char b = in.next().charAt(0);
        int ret = 0;

        if(a <= 1){
            ret = 20;

        }else {

            //超过1部分 - 向下取整结果,大于0,说明可以向上取整
            if(a - (int)(a-1) > 0) {
                ret = 20   (int)(a-1)   1;
            }else{
                ret  = 20   (int)(a-1); 
            }
 
        }
        

        if(b == 'y'){
            ret  = 5;
        }

        System.out.println(ret);
    } 

(2)我们也可以使用自带的向上取整库函数:ceil

代码:

代码语言:javascript复制
public static void main(String[] args) {
       
        Scanner in = new Scanner(System.in);
        double a = in.nextDouble();
        char b = in.next().charAt(0);
        int ret = 0;

        if(a <= 1){
            ret = 20;

        }else {
            //ceil函数可以完成向上取整
            ret = 20   (int)Math.ceil(a-1);
        }
        

        if(b == 'y'){
            ret  = 5;
        }

        System.out.println(ret);
    } 

0 人点赞