TIL | [알고리즘] 하샤드 수(Java)

hyemin·2022년 3월 16일
0

알고리즘

목록 보기
28/38
post-thumbnail

THINKING

  1. while문을 이용하여 x의 각 자리수를 구해 answer에 더해준다
    • x % 10을 통해 1의 자리를 구할 수 있다
  2. x % answer == 0이면 true를 아니면 false를 반환해준다

내 풀이

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

bool solution(int x) {
    int answer = 0;
    int temp = x;
    
    while (temp >= 1) {
        answer += temp % 10;
        temp /= 10;
    }
    return (x % answer == 0) ? true : false;
}

0개의 댓글

관련 채용 정보