※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※
이번에 볼 문제는 백준 24923번 문제인 Canadians, eh?이다.
문제는 아래 링크를 확인하자.
https://www.acmicpc.net/problem/24923
24923번: Canadians, eh?
The input consists of a single line, which is the sentence spoken by the suspect. The line will contain between $3$ and $100$ characters (inclusively). It will also only contain English letters, digits, spaces, and punctuation symbols.
www.acmicpc.net
주어지는 길이 3 이상의 문장 한 줄을 읽어, 마지막 세 글자가 "eh?"인지를 판단하는 문제이다.
getline을 이용하여 문장 한 줄을 읽고, substr을 이용하여 주어진 문자열의 마지막 세 글자로 구성된 부분 문자열을 쉽게 얻을 수 있다.
아래는 제출한 소스코드이다.
#include <iostream>
#include <string>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
string s; getline(cin, s);
int slen = s.length();
if (s.substr(slen - 3, 3) == "eh?") cout << "Canadian!";
else cout << "Imposter!";
}
728x90
'BOJ' 카테고리의 다른 글
[BOJ 24302 // C++] КУРИЕРИ (0) | 2022.05.01 |
---|---|
[BOJ 24924 // C++] Card Divisibility (0) | 2022.05.01 |
[BOJ 24977 // C++] Visits (0) | 2022.04.30 |
[BOJ 24978 // C++] Subset Equality (0) | 2022.04.29 |
[BOJ 24979 // C++] COW Operations (0) | 2022.04.28 |