• Home
  • About
    • Jiwon Jeong photo

      Jiwon Jeong

      끊임없이 배우며 성장하는 엔지니어

    • Learn More
    • Email
    • Github
  • Posts
    • All Posts
    • All Tags
    • All Categories
  • Projects

[프로그래머스] - 하샤드 수

12 May 2021

Reading time ~1 minute

풀이(성공)

중요

  • stoi 함수는 char이 아닌 String 자료형에서만 int로 적용가능
  • char형의 int변환은 st[i]- '0' 와 같은 형태로 가능!
#include <string>
#include <vector>

using namespace std;

bool solution(int x) {
    bool answer = true;
    string st = to_string(x);
    int sum=0;
    
    for(int i=0; i<st.length(); i++){
        int t=st[i]- '0';
        sum+=t;
    }
    
    if(x % sum ==0){
        answer=true;
    }else{
        answer=false;
    }
    return answer;
}




PS Share Tweet +1