Programmers Solutions/Level-1
[프로그래머스] 하샤드 수
률무차
2021. 2. 3. 04:02
title: "하샤드 수"
category: 프로그래머스[Level-1]
tags: [C++, JavaScript, 프로그래머스]
date: "2021-01-20"
문제 링크
C++
#include <string>
#include <vector>
using namespace std;
bool solution(int x) {
bool answer = true;
int sum = 0;
string str = to_string(x);
for(char c: str){
sum += (c - '0');
}
answer = (x%sum==0) ? true : false;
return answer;
}
JavaScript
function solution(x) {
var answer = true;
let sum = 0;
x.toString()
.split("")
.forEach((value) => {
sum += value - "0";
});
answer = x % sum === 0 ? true : false;
return answer;
}
728x90
반응형