반응형

코드포스(Codeforces) 71A Way Too Long Words 풀이 코드

C | C++ | Java | Python

Difficulty : *800 (Strings)


문제 해설

짧은 문자열은 그대로 출력, 10자 이상의 긴 문자열은 맨 앞 문자와 맨 뒷 문자, 중간 길이(숫자)로 출력하는 문제입니다.

풀이

입력받은 문자열의 길이가 10 이상일 경우 앞뒤 한 문자를 제외한 길이를 알아낸 후 앞 문자 + 중간 문자열 길이 + 뒷 문자 형식으로 출력합니다. 애초에 길이가 10 미만인 경우 그대로 출력합니다. 

코드

#include <stdio.h>
# include <stdlib.h>

int main (){
    int i, n, len;
    char word[100];
    scanf("%d",&n);
 
    for (i = 0; i < n; i++){
        scanf("%s",&word);
        len=strlen(word);
        if (len>10){
               printf("%c%d%c\n", word[0], len-2, word[len-1]);
            }
        else{
            printf("%s\n", word);
        }
    }
return 0;
}
#include<iostream>
using namespace std;
int main(){
    int n;
    string word;
    cin >> n;
    for(int i = 0; i < n; i++){
        cin >> word;
        if(word.size() <= 10){
            cout << word << endl;
            continue;
        }
        string newword;
        newword = word[0] + to_string(word.size() - 2) + word.back();
        cout << newword << endl;
    }
    return 0;
}
import java.util.*;
 
public class WayTooLongWords{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String words = null;
        int n = sc.nextInt();
        
        for(int i = 0; i <= n; i++){
            words = sc.nextLine();
            if(words.length()>10){
                System.out.print(words.charAt(0));
                System.out.print(words.length()-2);
                System.out.println(words.charAt(words.length()-1));
            }else {
                System.out.println(words);  
            }
        } 
    }
}
for i in [0]*int(input()):
    w=input();
    l=len(w)-2;
    print([w,w[0]+str(l)+w[-1]][l>8])

문제 출처

https://codeforces.com/contest/71/problem/A

반응형

+ Recent posts