[백준 2857] FBI (C#, C++) - soo:bak
작성일 :
문제 링크
설명
다섯 명의 요원 이름 중 ‘FBI’라는 문자열이 포함된 요원을 찾아 인덱스를 출력하는 문제입니다.
- 입력은 5줄로 주어지며, 각 줄에는 요원의 코드명이 포함되어 있습니다.
- 각 줄에서
'FBI'
라는 단어가 포함되어 있는지를 확인합니다. - 해당 문자열이 포함된 줄의 번호(1부터 시작)를 모두 공백으로 구분해 출력합니다.
- 만약, 단 한 명도 없다면
"HE GOT AWAY!"
를 출력합니다.
접근법
- 각 요원의 코드명을 입력받습니다.
- 문자열 내부에
'FBI'
라는 부분 문자열이 포함되어 있는지를 검사합니다. - 포함된 경우 인덱스를 결과 목록에 추가하고, 마지막에 출력 조건에 따라 출력합니다.
Code
[ C# ]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using System;
using System.Collections.Generic;
class Program {
static void Main() {
var result = new List<int>();
for (int i = 1; i <= 5; i++) {
string input = Console.ReadLine();
if (input.Contains("FBI"))
result.Add(i);
}
if (result.Count == 0)
Console.WriteLine("HE GOT AWAY!");
else
Console.WriteLine(string.Join(" ", result));
}
}
[ 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
29
30
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
vi ans;
bool isA = false;
for (int i = 0; i < 5; i++) {
string name; cin >> name;
if (name.find("FBI") != string::npos) {
isA = true;
ans.push_back(i + 1);
}
}
if (!isA) cout << "HE GOT AWAY!\n";
else {
for (size_t i = 0; i < ans.size(); i++) {
cout << ans[i];
if (i != ans.size() - 1) cout << " ";
else cout << "\n";
}
}
return 0;
}