본문 바로가기
IT/알고리즘

9] Leetcode 1281. Subtract the Product and Sum of Digits of an Integer

by 깻잎쌈 2020. 6. 22.
반응형
 

Subtract the Product and Sum of Digits of an Integer - 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

주어진 숫자를 자릿수 별로 쪼개는 문제다.

일의 자리부터 계산하고 떼버리고?를 

0이 될때까지 한다. 

 

곱하는 건 초기값이 1, 더하는 건 0으로 ~

class Solution {
public:
    int subtractProductAndSum(int n) {
        int product = 1;
        int sum = 0;
        while(n!=0){
            product *= n%10;
            sum += n%10;
            
            n /=10;
        }
        
        return product-sum;
        
    }
};

 

 

반응형

댓글