본문 바로가기

백준 문제풀기/JAVA

[백준 1475 JAVA 자바] 방 번호

 

# 6과 9는 뒤집어 이용할 수 있다

 

이 문제가 실버등급을 받은 부분이라고 생각합니다

 

1. 1,2,3,4,5,7,8,0 중에서 빈도 최대값

2. 6,9의 빈도 합 나누기 2 

3. 1과 2의 값을 비교해 더 큰값을 출력합시다

 

코드입니다

 

import java.util.Scanner;

public class Main {
	public static void main(String[] args){
		Scanner scan= new Scanner(System.in);
		
		String str = scan.next();
		
		int[] arr = new int[10];
		
		for(int j=0; j<str.length(); j++) {
			arr[Integer.parseInt(str.substring(j, j+1))] += 1;
			
		}
		
		double max = Math.ceil((double)(arr[6] + arr[9])/2);
		
		if (max < arr[0]) max = arr[0];
		if (max < arr[1]) max = arr[1];
		if (max < arr[2]) max = arr[2];
		if (max < arr[3]) max = arr[3];
		if (max < arr[4]) max = arr[4];
		if (max < arr[5]) max = arr[5];
		if (max < arr[7]) max = arr[7];
		if (max < arr[8]) max = arr[8];
		
		System.out.println((int)max);
    }
}

int[] arr = new int[10];

은 0~9까지 빈도를 체크하기위한 어레이입니다

 

그 다음 for문에서 빈도를 체크합니다

 

Math.ceil(); 함수는 올림함수입니다

예를 들어 입력된 숫자가 66999 라면

(2+3)/2 = 2.5니깐

3세트를 살 필요가 있거든요