처음 작성한 코드는 다음과 같다.
#include <iostream>
#include <string>
using namespace std;
int main(){
while(true){
string str;
cin >> str;
if(str=="end")
break;
bool high_word = true;
//조건1
bool aeiou = false;
for(int i=0; i<str.length(); i++){
if(str[i]=='a' || str[i]=='e' || str[i]=='i' || str[i]=='o' || str[i]=='u'){
aeiou = true;
break;
}
}
if(aeiou==false){
high_word = false;
}
//조건2
int count_aeiou = 0;
int count_rest = 0;
for(int i=0; i<str.length(); i++){
if(str[i]=='a' || str[i]=='e' || str[i]=='i' || str[i]=='o' || str[i]=='u'){
count_aeiou++;
if(count_aeiou >= 3){
high_word = false;
break;
}
}
else{
count_rest++;
if(count_rest >= 3){
high_word = false;
break;
}
}
}
//조건3
for(int i=1; i<str.length(); i++){
if( (str[i-1]==str[i]) && (str[i]!='e') && (str[i]!='o') ){
high_word = false;
break;
}
}
//출력
if(high_word == false)
cout << "<" << str << "> is not acceptable." << "\n";
else
cout << "<" << str << "> is acceptable." << "\n";
}
return 0;
}
하지만 틀렸다는 결과가 나왔다.
조건 2에서 모음이나 자음이 3번 이상 나오면 안되는 것 처럼 구현했는데, 그게 아니라 모음이나 자음이 3번 "연속"으로 오면 안되는 것이었다.
(+ 문자 1개 ex.'a'만 비교할때는 " "가 아니라 ' '를 사용해야 한다. 'a'처럼.)
다시 작성한 코드는 다음과 같다.
#include <iostream>
#include <string>
using namespace std;
int main(){
while(true){
string str;
cin >> str;
if(str=="end")
break;
bool high_word = true;
//조건1
bool aeiou = false;
for(int i=0; i<str.length(); i++){
if(str[i]=='a' || str[i]=='e' || str[i]=='i' || str[i]=='o' || str[i]=='u'){
aeiou = true;
break;
}
}
if(aeiou==false){
high_word = false;
}
//조건2
int count_aeiou = 0;
int count_rest = 0;
for(int i=0; i<str.length(); i++){
if(str[i]=='a' || str[i]=='e' || str[i]=='i' || str[i]=='o' || str[i]=='u'){
count_aeiou++;
count_rest = 0;
if(count_aeiou >= 3){
high_word = false;
break;
}
}
else{
count_rest++;
count_aeiou = 0;
if(count_rest >= 3){
high_word = false;
break;
}
}
}
//조건3
for(int i=1; i<str.length(); i++){
if( (str[i-1]==str[i]) && (str[i]!='e') && (str[i]!='o') ){
high_word = false;
break;
}
}
//출력
if(high_word == false)
cout << "<" << str << "> is not acceptable." << "\n";
else
cout << "<" << str << "> is acceptable." << "\n";
}
return 0;
}
'백준' 카테고리의 다른 글
백준 1205 등수 구하기 c++ (0) | 2024.04.02 |
---|---|
9655 돌 게임 c++ (0) | 2024.04.02 |
백준 10431 줄세우기 c++ (0) | 2024.03.31 |
백준 11723 집합 c++ (0) | 2024.03.30 |
백준 2003 수들의 합 2 c++ (0) | 2024.03.29 |