백준

[백준/BOJ] [C++] [26314번] Vowel Count

silencecolor 2025. 2. 28. 20:34
반응형

문제 페이지

https://www.acmicpc.net/problem/26314

 

 

문제

Dr. Orooji noticed that his name has more vowels than consonants. Since he likes meeting people like himself, he has asked you to write a program to help him identify such names.

Given a name, determine whether or not it has more vowels than consonants. Assume the vowels are “aeiou”.

입력

The first input line contains a positive integer, n, indicating the number of names to check. The names are on the following n input lines, one name per line. Each name starts in column 1 and consists of 1-20 lowercase letters (assume the name will not contain any other characters).

출력

Output each name on a separate line as it appears in the input followed by a 1 (one) or 0 (zero) on the next line indicating whether or not it has more vowels than consonants. Follow the format illustrated in Sample Output.

 

 

코드

#include <bits/stdc++.h> 
using namespace std;

int main()
{
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

	int n; 
    cin >> n;
    
	for (int i = 0; i < n; ++i)
	{
		string inputs;
		cin >> inputs;
		cout << inputs << '\n';

		int vowel = 0;
		int consonant = 0;
		for (int j = 0; j < inputs.size(); ++j)
		{
			switch (inputs[j])
			{
			case 'a':
			case 'i':
			case 'o':
			case 'u':
			case 'e':
				++vowel;
				break;
			default:
				++consonant;
				break;
			}
		}

		cout << ((vowel > consonant) ? 1 : 0) << '\n';
	}

	return 0;
}

 

코드 설명

입력 개수 n이 주어지고, string이 n개 주어진다. 

각 string 별로 자음/모음의 개수를 구분하고, 자음이 많다면 0, 모음이 많다면 1을 출력한다.

 

영어의 모음은 'a', 'i', 'o', 'u', 'e' 이므로 입력받은 문자열을 순회하면서 해당 알파벳이 자음인지 모음인지를 검증해 개수를 세고, 결과에 따라 값을 출력하면 된다.

 

반응형