[백준 9226] 도깨비말 (C#, C++) - soo:bak
작성일 :
문제 링크
설명
각 단어를 영어의 피그라틴어(Pig Latin) 규칙에 따라 변형하여 출력하는 문제입니다.
변형 규칙은 다음과 같습니다:
- 단어의 앞에서부터 처음 등장하는 모음까지, 그 앞부분을 잘라 단어의 뒤로 보냅니다.
- 그 뒤에
"ay"
를 붙여 단어를 완성합니다. - 단어의 맨 앞이 모음이라면, 단어는 그대로 두고
"ay"
만 붙입니다. - 단어에 모음이 아예 없는 경우에도, 그대로
"ay"
를 붙입니다.
접근법
- 입력된 단어를 순서대로 한 줄씩 처리합니다.
- 각 단어에서 앞에서부터
처음 나오는 모음 위치
를 찾습니다. - 해당 위치를 기준으로 두 부분으로 나눠 재조합합니다:
모음 이후 부분 + 모음 이전 부분 + ay
- 모음이 단어 처음에 있거나 아예 없는 경우는 전체 단어 뒤에
"ay"
만 붙입니다.
Code
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System;
class Program {
static void Main() {
while (true) {
var word = Console.ReadLine();
if (word == "#") break;
int idx = 0;
string vowels = "aeiou";
while (idx < word.Length && !vowels.Contains(word[idx]))
idx++;
string result = word.Substring(idx) + word.Substring(0, idx) + "ay";
Console.WriteLine(result);
}
}
}
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <bits/stdc++.h>
using namespace std;
bool isVowel(char c) {
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string s;
while (cin >> s && s != "#") {
size_t i = 0;
while (i < s.size() && !isVowel(s[i])) i++;
cout << s.substr(i) + s.substr(0, i) + "ay" << "\n";
}
return 0;
}