SWEA

SWEA 1926. 간단한 369게임 D2 C++

2024. 5. 11. 23:34

 

 

 

 

 

1. (1부터 N까지의) 각 숫자를 한 자리수 단위로 분해해서,

2. 각 자리수마다 3, 6, 9가 들어있는지 확인하고,

3. 3, 6, 9 를 포함한 횟수에 따라 출력하도록 했다.

작성한 코드는 다음과 같다.

#include<iostream>
using namespace std;

int main(int argc, char** argv){
    int n;
    cin >> n;
    
    for(int i=1; i<=n; i++) {
    	int count = 0;
        
        int num = i;
        while(num>0) {
            if( (num%10 == 3) || (num%10 == 6) || (num%10 == 9) )
                count++;
        	num /= 10;
        }
        
        if(count==0)
           cout << i << " ";
        else if(count==1)
           cout << "-" << " ";
        else if(count==2)
           cout << "--" << " ";
        else if(count==3)
           cout << "---" << " ";                
    }
    
	return 0;//정상종료시 반드시 0을 리턴해야합니다.
}