[백준 18142] Tapioka (C#, C++) - soo:bak
작성일 :
문제 링크
설명
세 단어로 이루어진 음식 이름에서 “bubble”과 “tapioka” 단어를 제거하고, 남은 단어들을 공백으로 이어 출력하는 문제입니다. 남는 단어가 없으면 “nothing”을 출력합니다.
접근법
입력된 세 단어를 순서대로 확인하며 bubble이나 tapioka가 아니면 결과 목록에 넣습니다.
결과가 비어 있으면 “nothing”, 그렇지 않으면 공백으로 join해 출력합니다.
Code
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using System;
using System.Text;
class Program {
static void Main() {
var parts = Console.In.ReadToEnd().Split((char[])null, StringSplitOptions.RemoveEmptyEntries);
var sb = new StringBuilder();
for (var i = 0; i < parts.Length; i++) {
var w = parts[i];
if (w == "bubble" || w == "tapioka") continue;
if (sb.Length > 0) sb.Append(' ');
sb.Append(w);
}
if (sb.Length == 0) Console.WriteLine("nothing");
else Console.WriteLine(sb.ToString());
}
}
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <bits/stdc++.h>
using namespace std;
typedef vector<string> vs;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
vs words(3);
for (int i = 0; i < 3; i++) cin >> words[i];
vs keep;
for (auto& w : words) {
if (w == "bubble" || w == "tapioka") continue;
keep.push_back(w);
}
if (keep.empty()) cout << "nothing\n";
else {
for (size_t i = 0; i < keep.size(); i++) {
if (i) cout << " ";
cout << keep[i];
}
cout << "\n";
}
return 0;
}