Post List

[BOJ] 백준 10769 행복한지 슬픈지

[BOJ] 백준 10769 행복한지 슬픈지



이 문제는 주어진 문자열 중 :-) 과 :-( 의 개수만 구하면 되는 문제 입니다.
저는 문자열의 처음부터 시작하여 "  :  " 를 찾았다면, 그 후에 등장하는것이 표정인지 체크하는 방법으로 풀었습니다.

소스 코드 :

#include 
#include 
using namespace std;

int main() {
 string a;
 getline(cin, a);
 int alen = a.length();
 int happy = 0;
 int sad = 0;
 bool c = false;
 for (int i = 0; i < alen; i++) {
        // : 을 만났을때, 그 다음문자가 표정문자라면.
  if (a[i] == ':') {
   if (i + 2 < alen) {
    if (a[i + 1] == '-' && a[i + 2] == ')') {
     happy++;
     c = true;
    }
    else if (a[i + 1] == '-' && a[i + 2] == '(') {
     sad++;
     c = true;
    }
   }
  }
 }
    // happy sad 둘중 하나도 안나왔다면
 if (!c) {
  cout << "none" << endl;
 }
 else {
  if (happy == sad) {
   cout << "unsure" << endl;
  }
  else {
   cout << (sad > happy ? "sad" : "happy") << endl;
  }
 }
}

댓글