https://www.acmicpc.net/problem/5622
1. 알파벳 배열의 인덱스로 시간을 구하는 방법
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Main{
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int[] arr = {2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,7,8,8,8,9,9,9,9};
String str = br.readLine();
int time=0;
for(int i=0; i<str.length(); i++){
int index = str.charAt(i)-65;
time+=arr[index]+1;
}
System.out.println(time);
}
}
2.if-else문 사용
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
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 tmp = str.charAt(i);
if(tmp>= 'A' && tmp <='C') time+=3;
else if(tmp>='D' && tmp<='F') time+=4;
else if(tmp>='G' && tmp<='I') time+=5;
else if(tmp>='J' && tmp<='L') time+=6;
else if(tmp>='M' && tmp<='O') time+=7;
else if(tmp>='P' && tmp<='S') time+=8;
else if(tmp>='T' && tmp<='V') time+=9;
else time+=10;
}
System.out.println(time);
}
}
'ALGORITHM' 카테고리의 다른 글
[JAVA] 백준 2417번- 정수 제곱근 (0) | 2022.08.02 |
---|---|
[JAVA] 백준 2941번- 크로아티아 알파벳 (0) | 2022.08.01 |
[JAVA] 백준 10825번- 국영수 (0) | 2022.07.30 |
[JAVA] 백준 10815번- 숫자 카드 (0) | 2022.07.30 |
[JAVA] 백준 1920번- 수 찾기 (0) | 2022.07.30 |