[SWEA] 올림픽 종목 투표

문제 보러가기

제한사항

이외의 제한사항은 없다.

첫번째 생각

B배열을 받아가면서 A배열과 비교하여 count를 센 후, 조건에 맞는 인덱스를 저장한 뒤 출력한다.

JAVA Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.util.Scanner;
public 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 N = sc.nextInt();
			int M = sc.nextInt();
			int[] ai = new int[N];
			int[] count = new int [N];
			int max = -1;
			int index = -1;
			for(int i=0; i<N; i++) {
				ai[i] = sc.nextInt();
			}
			
			for(int i=0; i<M; i++) {
				int b = sc.nextInt();
				for(int j=0; j<N; j++) {
					if( ai[j] <= b ) {
						count[j]++;
						if ( max < count[j] ) {
							max = count[j];
							index = j;
						}
						break;
					}
				}
			}
			
			System.out.println("#"+tc+" "+(index+1));
			
		}
	}
}