Last active
November 11, 2018 15:15
-
-
Save jayden-lee/1be0a60e8ef00fef717b672243962cb8 to your computer and use it in GitHub Desktop.
백준 알고리즘 1475번 방번호
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int index = 0; | |
for (char ch : roomNumber.toCharArray()) { | |
index = (int) ch - 48; | |
// 숫자 사용 횟수를 증가시킨다. | |
number[index] += 1; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for (int i = 0; i < 10; i++) { | |
// 숫자 9의 경우 6과 함께 카운트가 되었으므로 2로 나누어서 반올림한다. | |
if (i == 9) { | |
useCount = (int) Math.round(number[9] / 2.0); | |
} else { | |
useCount = number[i]; | |
} | |
if (useCount > maxUseCount) { | |
maxUseCount = useCount; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
/** | |
* 방 번호 문제<br> | |
* 알고리즘 분류 : 문자열 처리 | |
* | |
* @author jayden | |
*/ | |
public class Main { | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
// 각 숫자별(0~9) 사용한 횟수를 저장하는 배열을 생성한다. | |
int[] number = new int[10]; | |
String roomNumber = scanner.next(); | |
// 6과 9는 혼용되어서 사용할 수 있으므로 | |
// 6을 9로 값을 변경한다. | |
roomNumber = roomNumber.replace('6', '9'); | |
int index; | |
for (char ch : roomNumber.toCharArray()) { | |
index = (int) ch - 48; | |
// 숫자 사용 횟수를 증가시킨다. | |
number[index] += 1; | |
} | |
int maxUseCount = -1; | |
int useCount = 0; | |
for (int i = 0; i < 10; i++) { | |
// 숫자 9의 경우 6과 함께 카운트가 되었으므로 2로 나누어서 반올림한다. | |
if (i == 9) { | |
useCount = (int) Math.round(number[9] / 2.0); | |
} else { | |
useCount = number[i]; | |
} | |
if (useCount > maxUseCount) { | |
maxUseCount = useCount; | |
} | |
} | |
System.out.println(maxUseCount); | |
scanner.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment