Programing/백준, 프로그래머스(C++)
[C++][백준 4659] 비밀번호 발음하기
hye3193
2024. 12. 31. 22:11
https://www.acmicpc.net/problem/4659
제출 코드
#include <iostream>
#include <string>
using namespace std;
bool vowel(char a)
{
if (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')
return true;
else
return false;
}
int main()
{
string pwd;
string endVar = "end";
while (true)
{
bool cond1 = false;
bool res = true;
int count = 1;
char pastChar = 'n';
cin >> pwd;
if (pwd.compare(endVar) == 0)
break;
for (int i = 0; i < pwd.length(); i++)
{
if (vowel(pwd[i]))
{
cond1 = true;
count = (pastChar == 'v') ? count + 1 : 1;
pastChar = 'v';
}
else
{
count = (pastChar == 'c') ? count + 1 : 1;
pastChar = 'c';
}
if (count > 2) res = false;
if (pwd[i] == pwd[i - 1])
{
if (pwd[i] != 'e' && pwd[i] != 'o')
res = false;
}
}
if (!cond1) res = false;
if (res)
cout << '<' << pwd << '>' << " is acceptable." << endl;
else
cout << '<' << pwd << '>' << " is not acceptable." << endl;
}
}
조건 1을 처리하는 bool 변수를 따로 만들고, pastChar(초기 상태 n(none), 모음일 때 v(vowel), 자음일 때 c(consonant))으로 조건 2를 체크하고, 조건 3은 그냥 매 루프마다 이전 알파벳을 체크하는 방식으로 코드를 작성하였다
문자열끼리의 비교는 ==이나 != 연산자로 할 수 없고 string1.compare(string2) 형식으로 compare함수를 이용해서 비교해야 한다. 두 문자열이 일치하면 0을, 일치하지 않으면 -1을 반환한다