[백준 2789] 유학 금지 (C#, C++) - soo:bak
작성일 :
문제 링크https://soo-bak.github.io/algorithm/boj/forbiddenLetters-9/#문제-링크
설명https://soo-bak.github.io/algorithm/boj/forbiddenLetters-9/#설명
문자열에서 특정 문자들을 제거하여 출력하는 간단한 구현 문제입니다.
- 입력으로 주어진 문자열에서
CAMBRIDGE
에 포함된 모든 알파벳들을 제거한 결과를 출력해야 합니다. CAMBRIDGE
에 포함된 문자는 다음과 같습니다:C
,A
,M
,B
,R
,I
,D
,G
,E
- 이 문자들이 포함되어 있으면 제거하고, 나머지 문자만을 순서대로 출력합니다.
접근법https://soo-bak.github.io/algorithm/boj/forbiddenLetters-9/#접근법
- 입력 문자열을 읽어옵니다.
- 입력 문자열의 각 문자를 순회하며:
CAMBRIDGE
에 포함되지 않은 경우만 출력합니다.
- 최종적으로 남은 문자들을 공백 없이 그대로 출력합니다.
Codehttps://soo-bak.github.io/algorithm/boj/forbiddenLetters-9/#code
C#https://soo-bak.github.io/algorithm/boj/forbiddenLetters-9/#c
1
2
3
4
5
6
7
8
9
10
11
using System;
using System.Linq;
class Program {
static void Main() {
var forbidden = "CAMBRIDGE";
var input = Console.ReadLine();
var result = new string(input.Where(c => !forbidden.Contains(c)).ToArray());
Console.WriteLine(result);
}
}
C++https://soo-bak.github.io/algorithm/boj/forbiddenLetters-9/#c-1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string inspect = "CAMBRIDGE";
string str; cin >> str;
for (size_t i = 0; i < str.size(); i++) {
bool isI = false;
for (auto c: inspect) {
if (str[i] == c) isI = true;
}
if (!isI) cout << str[i];
}
cout << "\n";
return 0;
}