9298번 Ant Entrapment | 뭐라도 하겠지

9298번 Ant Entrapment

이왜틀?


남은 문제들은 파이썬으로 조져주자.
자바코드 많이보니까 멀미난다.

img01

9298번 Ant Entrapment - 백준

아니 이게 뭐지?
왜 이렇게 많이 틀렸지?

Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
for(int k=1;k<=t;k++) {
	int n=sc.nextInt();
	double maxx=Double.MIN_VALUE;
	double minx=Double.MAX_VALUE;
	double maxy=Double.MIN_VALUE;
	double miny=Double.MAX_VALUE;
	for(int i=0;i<n;i++) {
		double x=sc.nextDouble();
		double y=sc.nextDouble();
		if(x>maxx) maxx=x;
		if(x<minx) minx=x;
		if(y>maxy) maxy=y;
		if(y<miny) miny=y;
	}
	double x=maxx-minx;
	double y=maxy-miny;
	System.out.printf("Case %d: Area %.12g, Perimeter %.12g\n",k,x*y,(x+y)*2);
}

아하…
Double.MIN_VALUE는 사실 음수가 아니다!!!
입력은 최소 -1000.0 까지 들어올 수 있는데
최소값을 잘못 잡았으니 당연히 틀리는 것이다.

설계


똑같이 파이썬으로 구현하면 될 듯?

구현


T = int(input())

for t in range(T):

    N = int(input())

    max_x, max_y = -1001.0, -1001.0
    min_x, min_y = 1001.0, 1001.0

    for _ in range(N):
        x, y = map(float, input().split())
        max_x = max(max_x, x)
        min_x = min(min_x, x)
        max_y = max(max_y, y)
        min_y = min(min_y, y)
    
    x = max_x - min_x
    y = max_y - min_y

    print(f"Case {t+1}: Area {x * y :.9f}, Perimeter {2 * (x + y) :.9f}")

채점


img02

반성


역시 파이썬 코드는 예뻐 깔끔해 아름다워

코드 확인


Link to GitHub

Updated: