항해 99클럽 코테

99클럽 코테 스터디 13일차 TIL

metamong-data 2024. 11. 10. 10:03
728x90
반응형

오늘의 학습 키워드

해시

과제

문제

스페이스로 띄어쓰기 된 단어들의 리스트가 주어질때, 단어들을 반대 순서로 뒤집어라. 각 라인은 w개의 영단어로 이루어져 있으며, 총 L개의 알파벳을 가진다. 각 행은 알파벳과 스페이스로만 이루어져 있다. 단어 사이에는 하나의 스페이스만 들어간다.

입력

첫 행은 N이며, 전체 케이스의 개수이다.

N개의 케이스들이 이어지는데, 각 케이스는 스페이스로 띄어진 단어들이다. 스페이스는 라인의 처음과 끝에는 나타나지 않는다. N과 L은 다음 범위를 가진다.

  • N = 5
  • 1 ≤ L ≤ 25

출력

각 케이스에 대해서, 케이스 번호가 x일때 "Case #x: " 를 출력한 후 그 후에 이어서 단어들을 반대 순서로 출력한다.

예제 입력 1 복사

3
this is a test
foobar
all your base

예제 출력 1 복사

Case #1: test a is this
Case #2: foobar
Case #3: base your all

풀이 과정

  1. li[ : : -1 ] 은 정확하게, 주어진 리스트를 거꾸로 읊은 리스트를 반환한는 기능을 이용해서 쉽게 풀었다제출 코드 (python)
for i in range(int(input())): 
    n = input().split() 
    print(f"Case #{i+1}: {' '.join(n[::-1])}")

다른 언어로 문제 풀기

GO 언어

package main

import (
    "bufio"
    "fmt"
    "os"
    "strings"
)

func main() {
    var t int
    fmt.Scan(&t)

    scanner := bufio.NewScanner(os.Stdin)

    for i := 1; i <= t; i++ {
        scanner.Scan()
        words := strings.Fields(scanner.Text())

        // 단어 역순으로 만들기
        for j := 0; j < len(words)/2; j++ {
            words[j], words[len(words)-1-j] = words[len(words)-1-j], words[j]
        }

        fmt.Printf("Case #%d: %s\n", i, strings.Join(words, " "))
    }
}

C++

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

int main() {
    int t;
    cin >> t;
    cin.ignore(); 

    for(int i = 0; i < t; i++) {
        string line;
        getline(cin, line);


        vector<string> words;
        stringstream ss(line);
        string word;

        while(ss >> word) {
            words.push_back(word);
        }


        cout << "Case #" << i+1 << ":";


        for(int j = words.size()-1; j >= 0; j--) {
            cout << " " << words[j];
        }
        cout << endl;
    }

    return 0;
}
}

오늘의 회고

  • 문제에 익숙해 지는 중이다

출처 : https://www.acmicpc.net/problem/12605

728x90