-
[JAVA] 백준 1755 숫자 놀이알고리즘 공부/문제 풀이 2021. 9. 27. 13:35
https://www.acmicpc.net/problem/1755
1755번: 숫자놀이
79를 영어로 읽되 숫자 단위로 하나씩 읽는다면 "seven nine"이 된다. 80은 마찬가지로 "eight zero"라고 읽는다. 79는 80보다 작지만, 영어로 숫자 하나씩 읽는다면 "eight zero"가 "seven nine"보다 사전순으로
www.acmicpc.net
풀이
코드
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int M = sc.nextInt(); int N = sc.nextInt(); ArrayList<String> list = new ArrayList<>(); for(int i=M; i<=N;i++){ String convert = ""; if(i>=10){ convert += numToString(i/10); convert += " "; } convert += numToString(i%10); list.add(convert); } Collections.sort(list); StringBuilder sb = new StringBuilder(); int cnt = 0; for(int i=0;i<list.size();i++){ String str = list.get(i); String[] s = str.split(" "); int num = 0; if(s.length>1){ num += 10 * stringToInt(s[0]); num += stringToInt(s[1]); } else num += stringToInt(s[0]); sb.append(num+" "); cnt++; if(cnt%10==0) sb.append('\n'); } System.out.println(sb); } public static String numToString(int i){ switch(i){ case 0: return "zero"; case 1: return "one"; case 2: return "two"; case 3: return "three"; case 4: return "four"; case 5: return "five"; case 6: return "six"; case 7: return "seven"; case 8: return "eight"; case 9: return "nine"; default: return " "; } } public static int stringToInt(String s){ switch(s){ case "zero": return 0; case "one": return 1; case "two": return 2; case "three": return 3; case "four": return 4; case "five": return 5; case "six": return 6; case "seven": return 7; case "eight": return 8; case "nine": return 9; default: return -1; } } }
'알고리즘 공부 > 문제 풀이' 카테고리의 다른 글
[JAVA] 백준 9660 돌 게임 6 (0) 2021.09.27 [JAVA] 백준 15993 1, 2, 3 더하기 8 (0) 2021.09.27 [JAVA] 정올 1681 해밀턴 순환회로 (0) 2021.09.23 [JAVA] 백준 1786 찾기 (0) 2021.09.23 [JAVA] 백준 2042 구간 합 구하기 (0) 2021.09.22