코딩 테스트/Level 1
22. 하샤드 수
컴닥
2019. 10. 20. 23:30
반응형
https://programmers.co.kr/learn/courses/30/lessons/12947
앞서 풀었던 21.자릿수 더하기와 유사합니다. 참고.
파이썬
def solution(x):
return not (x % sum([int(i) for i in str(x)]))
def solution(x):
return not (x % sum(map(int, str(x))))
자바스크립트
function solution(x) {
return !(x % String(x).split('').map(Number).reduce((a, b) => a + b))
}
자바
class Solution {
public boolean solution(int x) {
int sum = 0;
for (char num: String.valueOf(x).toCharArray())
sum += (num -'0');
return x % sum == 0;
}
}
class Solution {
public boolean solution(int x) {
return 0 == x % String.valueOf(x).chars().map(n -> n -'0').sum();
}
}
코틀린
class Solution {
fun solution(x: Int): Boolean {
return 0 == x % (x.toString().chars().map { it - '0'.toInt() }.sum())
}
}
고
func solution(x int) bool {
num := x
sum := 0
for i := 0; x >= 1; i++ {
sum += (x % 10)
x /= 10
}
return num%sum == 0
}
C#
using System.Linq;
public class Solution
{
public bool solution(int n) =>
0 == n % n.ToString().ToCharArray().Aggregate(0, (a, b) => a + b - '0');
}
반응형