2018年第九届蓝桥杯Java程序设计本科B组决赛个人题解汇总:
https://blog.csdn.net/daixinliangwyx/article/details/90258768
第一题
标题:三角形面积
已知三角形三个顶点在直角坐标系下的坐标分别为:
(2.3, 2.5)
(6.4, 3.1)
(5.1, 7.2)
求该三角形的面积。
注意,要提交的是一个小数形式表示的浮点数。
要求精确到小数后3位,如不足3位,需要补零。
解法:用海伦公式就好了,三条边的边长为a、b、c,p=(a+b+c)/2,area=sqrt(p*(p-a)*(p-b)*(p-c))。
代码:
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());
}
}
}