Algorithm/C++ - 프로그래머스

프로그래머스 - 하샤드 수 C++

ㅇㅇ잉 2021. 2. 17. 21:01

자릿수의 합을 먼저 구해준 후, 자릿수의 합으로 나눠지면 true, 아님 false를 출력시킨다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <string>
#include <vector>
using namespace std;
 
bool solution(int x) {
    bool answer = true;
    int num=0;
    int tmp=x;
    while(x!=0){
        num+=x%10;
        x/=10;
    }
    
    if(tmp%num!=0) answer=false;
    return answer;
}
cs