2018 Ninth Blue Bridge Cup Java Programming Undergraduate Group B Final Triangle Area (Fill in the blank)

2018 Ninth Blue Bridge Cup Java Programming Undergraduate Group B Final Individual Problem Solution Summary:

https://blog.csdn.net/daixinliangwyx/article/details/90258768

 

first question

Title: Area of ​​a Triangle

The coordinates of the three vertices of the known triangle in the Cartesian coordinate system are:
(2.3, 2.5)
(6.4, 3.1)
(5.1, 7.2)

Find the area of ​​this triangle.

Note that what is being submitted is a floating point number represented as a decimal.
It is required to be accurate to 3 decimal places. If it is less than 3 digits, it needs to be filled with zeros.


Solution: Just use Heron's formula, the lengths of the three sides are a, b, c, p=(a+b+c)/2 , area=sqrt(p*(pa)*(pb)*(pc) ) .

Code:

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.*;


public class Main {
    public static InputReader in = new InputReader(new BufferedInputStream(System.in));
    public static PrintWriter out = new PrintWriter(System.out);

    public static void main(String[] args) {
        double a, b, c, p;
        a = getDis(2.3, 2.5, 6.4, 3.1);
        b = getDis(2.3, 2.5, 5.1, 7.2);
        c = getDis(6.4, 3.1, 5.1, 7.2);
        p = (a+b+c) / 2;
        DecimalFormat df = new DecimalFormat("0.000");
        out.println(df.format(Math.sqrt(p*(p-a)*(p-b)*(p-c))));
        out.flush();
        out.close();
    }

    static double getDis(double x1, double y1, double x2, double y2) {
        return Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

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

        public String nextLine() {
            String str = null;
            try {
                str = reader.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

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

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

        public Double nextDouble() {
            return Double.parseDouble(next());
        }

        public BigInteger nextBigInteger() {
            return new BigInteger(next());
        }

    }
}

Answer: 8.795

Guess you like

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