BOJ 5622 다이얼

1 개요[ | ]

BOJ 5622 다이얼
  • 규칙에 따라 문자에 대응하는 수를 출력하는 문제
  • 알고리즘 분류: 문자열 처리

2 C++[ | ]

#include <iostream>
#include <string>
using namespace std;

int char2num(char ch) {
    if(ch < 'D') return 2;
    if(ch < 'G') return 3;
    if(ch < 'J') return 4;
    if(ch < 'M') return 5;
    if(ch < 'P') return 6;
    if(ch < 'T') return 7;
    if(ch < 'W') return 8;
    return  9;
}

int main() {
    string s;
    cin >> s;
    int seconds = 0;
    for(int i=0; i<s.length(); i++) {
        char ch = s[i];
        int num = char2num(ch);
        seconds += num + 1;
    }
    cout << seconds << '\n';
}

3 Java[ | ]

import java.util.Scanner;
public class Main {
    private static int timeForChar(char ch) {
        if( ch < 'D' ) return 3;
        if( ch < 'G' ) return 4;
        if( ch < 'J' ) return 5;
        if( ch < 'M' ) return 6;
        if( ch < 'P' ) return 7;
        if( ch < 'T' ) return 8;
        if( ch < 'W' ) return 9;
        return 10;
    }
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        String word = sc.next();
        //System.out.println(word);
        
        int time = 0;
        for( int i=0; i<word.length(); i++ ) {
            time += timeForChar(word.charAt(i));
        }
        System.out.println(time);
    }
}

4 Perl[ | ]

sub timeForChar {
    $ch = shift;
    return 3 if ( $ch lt 'D' );
    return 4 if ( $ch lt 'G' );
    return 5 if ( $ch lt 'J' );
    return 6 if ( $ch lt 'M' );
    return 7 if ( $ch lt 'P' );
    return 8 if ( $ch lt 'T' );
    return 9 if ( $ch lt 'W' );
    return 10;
}

$n = <>;
$time = 0;
$time += timeForChar(substr($n, $_, 1)) for (0..(length $n)-2);
printf("%d\n", $time);

5 PHP[ | ]

<?php
function time_for_char($ch) {
    if( $ch < 'D' ) return 3;
    if( $ch < 'G' ) return 4;
    if( $ch < 'J' ) return 5;
    if( $ch < 'M' ) return 6;
    if( $ch < 'P' ) return 7;
    if( $ch < 'T' ) return 8;
    if( $ch < 'W' ) return 9;
    return 10;
}
$chs = str_split(rtrim(fgets(STDIN)));
$time = 0;
foreach($chs as $ch) {
    $time += time_for_char($ch);
}
echo $time;

6 Python[ | ]

#    ABCDEFGHIJKLMNOPQRSTUVWXYZ
s = "22233344455566677778889999"
sum = 0
for x in input():
    sum += ord(s[ord(x)-65])-47
print( sum )
#       A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
nums = [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]
sum = 0
for x in input():
    sum += nums[ord(x)-65]+1
print(sum)
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}