ALGORITHM

[JAVA] 백준 5622번- 다이얼

연듀 2022. 8. 1. 09:38

 

https://www.acmicpc.net/problem/5622

 

5622번: 다이얼

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

www.acmicpc.net

 

 

 

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);
    }
}