티스토리 뷰


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
반응형
댓글
04-30 14:53
링크