작성일 :

문제 링크

26314번 - Vowel Count

설명

소문자로 이루어진 이름에서 모음 개수가 자음 개수보다 많은지 판정하는 문제입니다.


접근법

각 이름에서 모음의 개수를 셉니다. 자음 개수는 전체 길이에서 모음 개수를 빼면 됩니다. 모음이 자음보다 많으면 1을, 그렇지 않으면 0을 출력합니다.


Code

C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using System;

class Program {
  static bool IsVowel(char c) => c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';

  static void Main() {
    var n = int.Parse(Console.ReadLine()!);

    for (var i = 0; i < n; i++) {
      var s = Console.ReadLine()!;
      var vowels = 0;
      foreach (var ch in s)
        if (IsVowel(ch)) vowels++;

      var consonants = s.Length - vowels;
      Console.WriteLine(s);
      Console.WriteLine(vowels > consonants ? 1 : 0);
    }
  }
}

C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <bits/stdc++.h>
using namespace std;

bool isVowel(char c) {
  return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n; cin >> n;

  for (int i = 0; i < n; i++) {
    string s; cin >> s;
    int vowels = 0;
    for (char ch : s)
      if (isVowel(ch)) vowels++;

    int consonants = s.size() - vowels;
    cout << s << "\n" << (vowels > consonants ? 1 : 0) << "\n";
  }

  return 0;
}

Tags: 26314, BOJ, C#, C++, 구현, 문자열, 백준, 알고리즘

Categories: ,