관리 메뉴

cococo-coding

10987번. 모음의 개수 (c++풀이) 본문

[BOJ] 코드 모음/C++_learning 문제집

10987번. 모음의 개수 (c++풀이)

_dani 2023. 12. 9. 01:00

최종코드

1. switch문 이용

#include <iostream>
#include <string>
using namespace std;

int main() {
    //1. 단어를 입력받는다.
    //2. 모음의 개수를 센다.
    //3. 모음의 개수를 출력한다.

    //1
    string word;
    cin >> word;

    //2
    int len=word.length();
    int cnt=0;
    for(int i=0; i<len; i++){
        switch (word[i]) {
            case 97:
                cnt++;
                break;
            case 101:
                cnt++;
                break;
            case 105:
                cnt++;
                break;
            case 111:
                cnt++;
                break;
            case 117:
                cnt++;
                break;
            default:
                continue;
                
        }
    }

    //3
    cout << cnt << endl;
    return 0;
}

 

2. if문 이용

#include <iostream>
#include <string>
using namespace std;

int main() {
    //if문 버전
    //1. 단어를 입력받는다.
    //2. 모음의 개수를 센다.
    //3. 모음의 개수를 출력한다.

    //1
    string arr;
    cin >> arr;

    //2
    int len=arr.length();
    int cnt=0;
    for(int i=0; i<len; i++){
       if(arr[i]==97 || arr[i]==101 || arr[i]==105 || arr[i]==111 || arr[i]==117){    
        cnt++;             
        }
    }
    
    //3
    cout << cnt << endl;
    return 0;
}

 

 

나는 switch-case문으로 코드를 작성했는데, 구글링을 해보니 if문으로 조건식에 or연산자로 5가지 조건을 한꺼번에 받고 cnt를 증가시키는 방법도 있어서 그 버전으로도 풀어봤다. 

2023.12.09