728x90
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
처음에는 max 값을 찾아 cnt값이 2가 되는
결과를 찾아줬었는데 그럴 필요가 없었다.

1. 첫 번째 코드
import java.util.Scanner;
class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int tc = 1; tc <= t; tc++) {
int max = Integer.MIN_VALUE;
int arr[] = new int[4];
for (int i = 0; i < 4; i++) {
arr[i] = sc.nextInt();
max = Math.max(max, arr[i]);
}
int[] cnt = new int[max + 1];
for (int i = 0; i < 2; i++) {
for (int j = arr[i*2]; j < arr[i*2 + 1]; j++) {
cnt[j]++;
}
}
int second=0;
for(int i=0;i<max;i++) {
if(cnt[i]==2) {
second++;
}
}
System.out.println("#"+tc+" "+second);
}
}
}
2. 두 번째 코드
import java.util.Scanner;
class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int tc = 1; tc <= t; tc++) {
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int d = sc.nextInt();
int start = Math.max(a, c);
int end = Math.min(b, d);
System.out.println("#"+tc+" "+((end-start)>0?(end-start):0));
}
}
}728x90
'SW Expert Academy > SWEA D3' 카테고리의 다른 글
| [SW Expert Academy] 최대 상금 (D3) (1) | 2024.10.13 |
|---|---|
| [SW Expert Academy] 숫자 조작 (D3) (0) | 2024.10.13 |
| [SW Expert Academy] 홀수 피라미드 (D3) (1) | 2024.10.13 |
| [SW Expert Acacademy] 피보나치 수 분배 (D3) (0) | 2024.10.13 |
| [SW Expert Academy] 명진이와 동휘의 숫자 맞추기 (D3) (0) | 2024.10.13 |