[백준] 6841

당당·2023년 7월 6일
0

백준

목록 보기
178/179

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

📔문제

Text messaging using a cell phone is popular among teenagers. The messages can appear peculiar because short forms and symbols are used to abbreviate messages and hence reduce typing.

For example, “LOL” means “laughing out loud” and “:-)” is called an emoticon which looks like a happy face (on its side) and it indicates chuckling. This is all quite a mystery to some adults.

Write a program that will continually input a short form and output the translation for an adult using the following translation table:

Short FormTranslation
CUsee you
:-)I’m happy
:-(I’m unhappy
;-)wink
:-Pstick out my tongue
(~.~)sleepy
TAtotally awesome
CCCCanadian Computing Competition
CUZbecause
TYthank-you
YWyou’re welcome
TTYLtalk to you later

📝입력

The user will be prompted to enter text to be translated one line at a time. When the short form “TTYL” is entered, the program ends. Users may enter text that is found in the translation table, or they may enter other words. All entered text will be symbols or upper case letters. There will be no spaces and no quotation marks.


📺출력

The program will output text immediately after each line of input. If the input is one of the phrases in the translation table, the output will be the translation; if the input does not appear in the table, the output will be the original word. The translation of the last short form entered “TTYL” should be output.


📝예제 입력 1

CCC
:-)
SQL
TTYL

📺예제 출력 1

Canadian Computing Competition
I’m happy
SQL
talk to you later

🔍출처

Olympiad > Canadian Computing Competition & Olympiad > 2007 > CCC 2007 Junior Division 2번


🧮알고리즘 분류

  • 구현
  • 문자열

📃소스 코드

import java.util.HashMap;
import java.util.Scanner;

public class Code6841 {
    public static void main(String[] args) {
        HashMap<String, String> txt=new HashMap<>();
        txt.put("CU","see you");
        txt.put(":-)","I’m happy");
        txt.put(":-(","I’m unhappy");
        txt.put(";-)","wink");
        txt.put(":-P","stick out my tongue");
        txt.put("(~.~)","sleepy");
        txt.put("TA","totally awesome");
        txt.put("CCC","Canadian Computing Competition");
        txt.put("CUZ","because");
        txt.put("TY","thank-you");
        txt.put("YW","you’re welcome");
        txt.put("TTYL","talk to you later");


        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
            String s=sc.next();
            if(s.equals("TTYL")){
                System.out.println(txt.get(s));
                break;
            }
            else{
                System.out.println(txt.getOrDefault(s,s));
            }
        }
    }
}

📰출력 결과


📂고찰

각각의 short Form에 해당되는 Translation을 해시맵에 넣어두었다.
그 다음 출력할 때 만약 txt 해시맵에 있으면 그것을 가져오고 아니면 입력받은 문자열 그대로 출력하면 된다.

TTYL을 입력받으면, 변역을 출력한 후, 대화를 종료하면 된다.

profile
MySQL DBA 신입 지원

0개의 댓글