AcWing-739. 数组选择


AcWing-739-数组选择

这里主要是为了记录如何格式化输出以及比较浮点数的大小。因为在计算机中不是所有浮点数都可以精确的表示,所以在比较浮点数时,通常会让两个浮点数之差小于一个很小的数,达到比较的效果。
而格式化输入也只需要指定格式和要输出的对象即可。

import java.io.*;
import java.util.*;

public class Main {
    public static final double zero = (1e-6);
    public static void main(String[] args) {
        Reader r = new Reader();
        Writer w = new Writer();
        double[] arr = new double[100];
        for(int i = 0; i < 100; i++) {
            arr[i] = r.nextDouble();
        }
        for(int i = 0; i < 100; i++) {
            if((arr[i] - 10) < zero) {
                w.printf("A[%d] = %.1f\n",i, arr[i]);
            }
        }
        w.flush();
        r.close();
        w.close();
    }
    
    public static class Reader {
        public BufferedReader in;
        public StringTokenizer tokenizer;

        public Reader() {
            in = new BufferedReader(new InputStreamReader(System.in), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(in.readLine());
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            }
            return tokenizer.nextToken();
        }

        public String nextLine() {
            String str = null;
            try {
                str = in.readLine();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            return str;
        }

        public boolean hasNext() {
            while (!tokenizer.hasMoreTokens()) {
                String str = nextLine();
                if (str == null) {
                    return false;
                }
                tokenizer = new StringTokenizer(str);
            }
            return true;
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }
        public void close() {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static class Writer {
        public PrintWriter out;

        public Writer() {
            this.out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
        }

        public void flush() {
            out.flush();
        }

        public void print(char obj) {
            out.write(obj);
        }

        public void print(String obj) {
            out.write(obj);
        }

        public void println(String str) {
            out.write(str + "\n");
        }

        public void println(Object obj) {
            out.write(obj + "\n");
        }

        public void printf(String format, Object... objs) {
            out.printf(format, objs);
        }

        public void close() {
            out.close();
        }
    }
}