题解 | #求解立方根#

求解立方根

https://www.nowcoder.com/practice/caf35ae421194a1090c22fe223357dca

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static java.util.Arrays.*;
import static java.util.stream.Stream.*;


public class Main {
    public static void main(String[] args) throws IOException {

        testTh();
    }
    private static void testTh() throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String str;
        StringBuilder sb = new StringBuilder();
        DecimalFormat df = new DecimalFormat("#0.0");
        while ((str = bf.readLine()) != null) {
            double parseDouble = Double.parseDouble(str);
            int flag = parseDouble >= 0 ? 1 : 0;
            double min = Double.MAX_VALUE;
            double tempo = 0;
            if (flag == 1) {
                for (double i = 0.1; i <= 2.8 ; i = i + 0.1) {
                    double res = i * i * i;
                    double v = parseDouble - Double.parseDouble(df.format(res));
                    if(v<0)v=-v;
                    if (v < min) {
                        min = v;
                        tempo = i;
                    }
                }
            }
            if (flag == 0) {
                for (double i = -0.1; i >= -2.8 ; i = i - 0.1) {
                    double res = i * i * i;
                    double v = parseDouble - Double.parseDouble(df.format(res));
                    if(v<0)v=-v;
                    if (v < min) {
                        min = v;
                        tempo = i;
                    }
                }
            }
            System.out.println(df.format(tempo));

        }

    }


}

全部评论

相关推荐

野猪不是猪🐗:这种直接口头上答应,骗面试,面完了直接拉黑,相当于给自己攒面经了(
点赞 评论 收藏
分享
2024-12-05 15:53
中南大学 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务