#4673번 셀프넘버 (c++)

2020. 2. 8. 17:39


#include <iostream>
using namespace std;

int check(int a)
{
    return a+(a/1000)+(a/100)%10+(a/10)%10+a%10;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    bool list[10000]; // 0 ~ 9999
    
    for(int i=0; i<10000; i++)
    {
        list[i]=false;
    }
    
    for(int i=0; i<10000; i++)
    {
        int num = check(i);
        
        if(num<=10000)
            list[num] = true; // true면 생성자가 있음, false가 셀프 넘버
    }
    
    for(int i=0; i<10000; i++)
    {
        if(list[i]==false)
            cout << i << "\n";
    }
    
    return 0;
}

용량 많이 안잡아먹는 bool문이 최고다.

굳이 vector만 먼저 생각하지 말자. 

'🖥️ CS > Baekjoon Algorithms' 카테고리의 다른 글

#1712번 손익분기점 (c++)  (0) 2020.02.12
#1316번 그룹단어체커 (c++)  (0) 2020.02.11
#1065번 한수 (c++)  (0) 2020.02.08
#1008번 A/B (c++)  (0) 2020.01.14
#4604번 Steganography (c++)  (0) 2020.01.13