[Programmers] 하샤드 수 (C++)
Algorithm2021. 11. 25. 17:03
반응형
코딩테스트 연습 - 하샤드 수
양의 정수 x가 하샤드 수이려면 x의 자릿수의 합으로 x가 나누어져야 합니다. 예를 들어 18의 자릿수 합은 1+8=9이고, 18은 9로 나누어 떨어지므로 18은 하샤드 수입니다. 자연수 x를 입력받아 x가 하
programmers.co.kr
이전에 푼 알고리즘 문제를 참고함.
[Leetcode] 258. Add Digits (C++)
Add Digits - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com class Solution { publ..
gold-goose.tistory.com
bool solution(int x)
{
bool answer = true;
int tmp;
int sum = 0;
int X = x;
while(x != 0) // 각 자리수를 더하는 과정
{
tmp = x%10;
sum += tmp;
x = x / 10;
}
if(X % sum != 0)
answer = false;
return answer;
}
반응형
'Algorithm' 카테고리의 다른 글
[백준] 1476. 날짜 계산 (0) | 2021.11.26 |
---|---|
[백준] 1712. 손익분기점 (C++) (8) | 2021.11.25 |
[Leetcode] 258. Add Digits (C++) (0) | 2021.11.25 |
[Leetcode] 342. Power of Four (C++) (0) | 2021.11.25 |
[백준] C++ 2775. 부녀회장이 될테야 (0) | 2021.11.24 |
댓글()