본문 바로가기
백준/5001 - 10000

[백준] 5602번 : 問題1(JAVA)

by lms0806 2021. 8. 21.
728x90
반응형

https://www.acmicpc.net/problem/5602

 

5602번: 問題1

1 行目は生 徒の人数 n と旅行候補の場所の数 m が空白で区切られ, i + 1 行に生徒 i のアン ケート結果を ○ は 1 で×は 0 で表し,空白で区切られた m 個の数字が並んでいる. 1 ≤ n ≤ 1000, 1

www.acmicpc.net

풀이

m만큼의 배열을 만들고, n번 입력받으면서 m번째로 들어오는 수가 1이면 ++ 합니다.

맨처음 max는 n이 되므로 max를 줄여가면서 같을 경우 StringBuilder에 추가해 준 후 마지막에 출력해주는식으로 풀면 됩니다.

 

소스코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
		StringTokenizer st = new StringTokenizer(br.readLine());
		
		int n = Integer.parseInt(st.nextToken()), m = Integer.parseInt(st.nextToken());
		
		int[] num = new int[m];
		
		for(int i = 0; i < n; i++) {
			st = new StringTokenizer(br.readLine());
			for(int j = 0; j < m; j++) {
				if(Integer.parseInt(st.nextToken()) == 1) {
					num[j]++;
				}
			}
		}
		
		int max = n;
		StringBuilder sb = new StringBuilder();
		while(max != 0) {
			for(int i = 0; i < m; i++) {
				if(max == num[i]) {
					sb.append(i + 1).append(" ");
				}
			}
			max--;
		}
		System.out.print(sb);
	}
}
728x90
반응형

댓글