하샤드 수
https://programmers.co.kr/learn/courses/30/lessons/12947
코딩테스트 연습 - 하샤드 수
양의 정수 x가 하샤드 수이려면 x의 자릿수의 합으로 x가 나누어져야 합니다. 예를 들어 18의 자릿수 합은 1+8=9이고, 18은 9로 나누어 떨어지므로 18은 하샤드 수입니다. 자연수 x를 입력받아 x가 하
programmers.co.kr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <vector> | |
using namespace std; | |
bool solution(int x) { | |
string num_str = to_string(x); | |
int sum = 0; | |
for(char c : num_str) | |
{ | |
sum += (c - 48); | |
} | |
return (x % sum) == 0; | |
} |
반응형
'자료구조 와 알고리즘 > 프로그래머스' 카테고리의 다른 글
콜라츠 추측 (0) | 2022.05.08 |
---|---|
평균 구하기 (0) | 2022.05.08 |
핸드폰 번호 가리기 (0) | 2022.05.08 |
행렬의 덧셈 (0) | 2022.05.08 |
x만큼 간격이 있는 n개의 숫자 (0) | 2022.05.08 |