Notice
Recent Posts
Recent Comments
Link
«   2025/12   »
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
Tags more
Archives
Today
Total
관리 메뉴

jeongwon

[백준 JAVA] 5622번 - 다이얼 본문

오늘의 문제

[백준 JAVA] 5622번 - 다이얼

jeongwon_ 2022. 6. 8. 14:50

문제:

 

5622번: 다이얼

첫째 줄에 알파벳 대문자로 이루어진 단어가 주어진다. 단어의 길이는 2보다 크거나 같고, 15보다 작거나 같다.

www.acmicpc.net

 

약식 순서도:

1) 문자열을 입력 받아 변수에 저장한다. (BufferedReader, String str)

2) 문자열의 각 문자 범위를 확인해 다이얼 소요 시간을 연산한다. (int time)

 

나의 답안: 

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


public class Main{
	
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String str = br.readLine();
		
		int time = 0;
		for(int i=0; i<str.length(); i++) {
			char ch = str.charAt(i);
			if(ch < 'D') time+= 3;
			else if(ch < 'G') time+=4;
			else if(ch < 'J') time+=5;
			else if(ch < 'M') time+=6;
			else if(ch < 'P') time+=7;
			else if(ch < 'T') time+=8;
			else if(ch < 'W') time+=9;
			else if(ch <= 'Z') time+=10;
			else time+=11;
			}
		System.out.println(time);
		br.close();
	}
}

 

개선 답안: (출처) https://st-lab.tistory.com/67

import java.io.IOException;

public class Main {
	public static void main(String[] args) throws IOException {
		
		int time = 0;
		int input;
		
		while(true) {
			
			input = System.in.read();
			
			if(input == '\n') {
				break;
			}
			
			if(input < 68) time += 3;
			else if(input < 71) time += 4;
			else if(input < 74) time += 5;
			else if(input < 77) time += 6;
			else if(input < 80) time += 7;
			else if(input < 84) time += 8;
			else if(input < 87) time += 9;
			else time += 10;
			
			
		}
		System.out.print(time);
		
	}
}

기본 입력 방식을 써본다는 것이 자꾸 잊는다. 오늘도 도움주신 lab 필자님께 감사를..