반응형
Approach
출처: https://www.acmicpc.net/problem/2789
문자열이 주어졌을 때,
{ 'C','A','M','B','R','I','D','G','E' }에 해당하는 부분은
제외하고 다시 출력하는 문제이다.
BOJ 특성상 데이터를 따로 저장하지 않고 바로 출력하면 된다.
#include <iostream>
#include <string>
using namespace std;
string str;
char forbidden[9 + 1] = { 'C','A','M','B','R','I','D','G','E' };
bool check(char ch)
{
for (int i = 0; i <= 9; ++i)
{
if (forbidden[i] == ch)
{
return false;
}
}
return true;
}
int main()
{
// freopen("input.txt", "r", stdin);
cin >> str;
for (int i = 0; i < str.length(); i++)
{
if (check(str[i]))
{
cout << str[i];
}
}
puts("");
}
Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String limit = "CAMBRIDGE";
String str = sc.next();
for(int i=0; i<str.length(); i++) {
// 입력받은 문자열을 한 개씩 추출
String target = str.substring(i, i+1);
boolean isLimit = false;
for(int j=0; j<limit.length();j++) {
String limitStr = limit.substring(j,j+1);
if(limitStr.equals(target)) {
isLimit = true;
}
}
// 만약 문자자체를 return해야 하는 것이라면
// concat(), substring()을 활용해야 하지만 바로 출력하면 된다.
if(isLimit) {
continue;
}
System.out.print(target);
}
}
}
반응형
'PS 문제 풀이 > Baekjoon' 카테고리의 다른 글
[BOJ] 백준 2864 5와 6의 차이 (0) | 2022.01.23 |
---|---|
[BOJ] 백준 2839 설탕배달 (0) | 2022.01.21 |
[BOJ] 백준 2775 부녀회장이 될테야 (0) | 2022.01.17 |
[BOJ] 백준 2740 행렬 곱셈 (0) | 2022.01.15 |
[BOJ] 백준 14935 FA (0) | 2022.01.15 |
댓글