본문 바로가기

알고리즘 문제 풀이/Programmers

하샤드 수 - 1단계 (C, C++)

반응형
SMALL
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

bool solution(int x) {
	bool answer = true;
    int x2 = x;
    int s = 0;
    while (x) {
    	s += x % 10;
        x /= 10;
    }
    if (x2 % s != 0) answer = false;
    return answer;
}

#include <iostream>

using namespace std;

bool solution(int x) {
	bool answer = true;
    int s = 0;
    int temp;
    temp = x;
    while (temp) {
    	s+= temp % 10;
        temp /= 10;
    }
    if (x%s ==0) answer = true;
    else answer = false;
    return answer;
}
반응형
LIST