리뷰
find를 사용하여 문제를 풀면 된다.
문제 풀이
- getline을 통해 한줄 전체를 문자열 s로 받아준다.
- s를 돌며 :-)를 찾으면 cnt1을 1만큼 올려주고 발견 위치 다음부터 다시 탐색한다. 없을 경우 break
- 마찬가지로 :-( 또한 찾아준 뒤 cnt2를 올려준다.
- cnt1와 cnt2를 비교하여 적절한 문자열을 출력해 주면 된다.
참고 사항
없음
정답 코드
#include <iostream>
#include <string>
using namespace std;
int main() {
int cnt1 = 0, cnt2 = 0;
string s;
getline(cin, s);
int c = 0;
while (1) {
int a = s.find(":-)", c);
if (a == -1) break;
cnt1++;
c = a + 1;
}
c = 0;
while (1) {
int a = s.find(":-(", c);
if (a == -1) break;
cnt2++;
c = a + 1;
}
if (!cnt1 && !cnt2) cout << "none";
else if (cnt1 > cnt2) cout << "happy";
else if (cnt1 < cnt2) cout << "sad";
else cout << "unsure";
}
728x90
'알고리즘 공부 > C++' 카테고리의 다른 글
백준 5555번 반지 C++ (0) | 2024.07.25 |
---|---|
백준 9507번 Generations of Tribbles C++ (0) | 2024.07.25 |
백준 5347번 LCM C++ (3) | 2024.07.22 |
백준 1940번 주몽 C++ 투 포인터 (4) | 2024.07.22 |
백준 1159번 농구 경기 C++ (0) | 2024.07.21 |