[프로그래머스 / C++] 하샤드 수

Seulguo·2022년 7월 7일
0

Algorithm

목록 보기
17/185
post-thumbnail

🐣 문제

링크 : https://school.programmers.co.kr/learn/courses/30/lessons/12947


🐥 코드

#include <string>
#include <vector>

using namespace std;

bool solution(int x) {
    bool answer = true;
    string str = to_string(x);
    vector<int> n;
    int sum = 0;
    
    for(char ch : str){
        n.push_back(ch - '0');  
    }
    
    for(int i = 0; i < n.size(); i++){
        sum += n[i];
    }
    
    if(x % sum == 0){
        answer = true;
    }
    else answer = false;
    
    return answer;
}

0개의 댓글