
-Code
import java.util.Scanner;
public class BOJ3602 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int b = sc.nextInt();
int w = sc.nextInt();
int res = -1;
int maxSide = (int) Math.sqrt(b + w);
for (int side = 1; side <= maxSide; side++) {
int total = (side * side);
// 총 갯수가 홀수인 경우 1개 차이를 내기 위해 아래처럼 계산
int maxCnt = (total + 1) / 2;
int minCnt = total / 2;
// 각 갯수가 색마다 가지고 있는 갯수마다 작아야함
if (Math.max(b, w) >= maxCnt && Math.min(b, w) >= minCnt) {
res = side;
}
}
System.out.println(res == -1 ? "Impossible" : res);
}
}'Beakjoon&프로그래머스 > Java' 카테고리의 다른 글
| [백준/Java] 11050번 이항 계수 1 (0) | 2025.12.15 |
|---|---|
| [백준/Java] 4575번 Refrigerator Magnets (0) | 2025.12.15 |
| [백준/Java] 11117번 Letter Cookies (0) | 2025.12.13 |
| [백준/Java] 10675번 Cow Routing (0) | 2025.12.12 |
| [백준/Java] 34797번 GPA Computation (0) | 2025.12.11 |