Huawei Online Programming Question Series-7-Approximate Values


Problem Description:

Problem Description

1. The question involves knowledge points.

Round-up and round-down of floats.
Math.ceil(key)Round up 5.6 –> 6
Math.floor(key)round down 5.6 –> 5

2. Solve it yourself.

  • Get one decimal place key*10%10.
package com.chaoxiong.niuke.huawei;
import java.util.Scanner;
/**
 * Create by tianchaoxiong on 18-4-8.
 */
public class HuaWei_7 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            float in = scanner.nextFloat();
            int key = (int) (in*10)%10;
            if(key<5)
                System.out.println((int)Math.floor(in));
            else
                System.out.println((int)Math.ceil(in));
        }
    }
}

3. Quality answers.

null

4. Summary of this question.

Math.ceil(key)Round up 5.6 –> 6
Math.floor(key)Round down 5.6 –> 5

public static double abs(double a),               --------------abs方法求绝对值
public static native double acos(double a)        -------------acos求反余弦函数
public static native double asin(double a)        -------------asin求反正切函数
public static native double atan(double a)        -------------atan求反正切函数
public static native double ceil(double a)        -------------ceil返回值最小的大于a的整数
public static native double cos(double a)        -------------cos求余弦函数
public static native double exp(double a)        -------------exp求e的a次幂
public static native double floor(double a)        -------------floor返回最大的小于a的数
public static native double log(double a)        -------------log返回lna
public static native double pow(double a,double b)------pow求a的b次幂
public static native double sin(double a)        -------------sin求正弦函数
public static native double sqrt(double a)      -------------sqrt求a的开平方
public static native double tan(double a)      -------------tan求正切函数
public static synchronized double random() -------------返回0~1的随机数

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325967980&siteId=291194637