Problem A Liu Hai(2018acm热身赛)

 

/*Problem A Liu Hai
Time limit:1seconds
Every one knows that iphone x has liu hai.
每个人都知道iphone x有刘海
Suppose liu hai is a polygon like that.
假设刘海是这样的一个多边形
What's the area of iphone x's liu hai?
iphone x的刘海的面积是多少?
Input
输入
Just one case.
只有一个案例
First line a single integer n(1<=n<=10^5).
第一行一个整数n(1<=n<=10^5).
Then n lines, each line contains two integer x,y.
然后n行,每一行包含两个整数x,y。
We guarantee that x is increasing.
我们保证x在增加。
Output
输出
Print the answer with two decimal numbers.
输出答案保留两位小数。
Sample Input
3
0 0
2 2
4 0
Sample Output
4.00
 */
import java.util.Scanner;
public class A {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //输入坐标个数
        int n = sc.nextInt();
        double num =0;
        int[][] arr = new int[n][2];
        //输入坐标
        for(int i=0;i<n;i++){
            arr[i][0] = sc.nextInt();
            arr[i][1] = sc.nextInt();
        }
        for(int i=0;i<n-1;i++){
            num+=area(arr[i][0],arr[i][1],arr[i+1][0],arr[i+1][1]);
        }
        System.out.printf("%.2f",num);
    }
   //计算两点之间形成的图形面积
    public static double area(int x1,int y1,int x2,int y2){
        x1 = x2 - x1;
        y1 = y1+y2;
        return x1*y1/2.0;
    }
}
发布了17 篇原创文章 · 获赞 26 · 访问量 7445

猜你喜欢

转载自blog.csdn.net/qq_41629684/article/details/89524008