[C++][백준 4659] 비밀번호 발음하기

PublicMinsu·2024년 1월 12일

문제

접근 방법

3가지 조건이 맞는지 확인해 주면 된다.
조건이 맞는지 아닌지를 확인하여 출력해 주면 된다.

코드

#include <iostream>
using namespace std;
string password;
bool isAcceptable()
{
    int cnt1 = 0, cnt2 = 0;
    bool isFind = false;
    for (int i = 0; i < password.size(); ++i)
    {
        if (!(password[i] == 'e' || password[i] == 'o')) // e 또는 o가 아닌 경우
        {
            if (i + 1 < password.size() && password[i] == password[i + 1]) // 연속하는지 확인
            {
                return false;
            }
        }
        if (password[i] == 'a' || password[i] == 'e' || password[i] == 'i' || password[i] == 'o' || password[i] == 'u') // 모음인 경우
        {
            isFind = true;
            ++cnt1;
            cnt2 = 0;
            if (cnt1 == 3)
            {
                return false;
            }
        }
        else // 자음인 경우
        {
            ++cnt2;
            cnt1 = 0;
            if (cnt2 == 3)
            {
                return false;
            }
        }
    }
    return isFind;
}
int main()
{
    ios::sync_with_stdio(0), cin.tie(0);
    while (true)
    {
        cin >> password;
        if (password == "end")
        {
            break;
        }
        cout << "<" << password << "> is " << (isAcceptable() ? "acceptable." : "not acceptable.") << "\n";
    }
    return 0;
}

풀이

모음을 세어주다가 자음이 나온다면 세어준 모음은 0으로 초기화해주고 자음은 1증가시켜주면 된다.
반대의 경우도 마찬가지이다.

최종적으론 모음이 사용됐는지를 여부로 판단해 주면 된다.

profile
연락 : publicminsu@naver.com

0개의 댓글