코린이의 소소한 공부노트

[LeetCode/Easy] 1281. Subtract the Product and Sum of Digits of an Integer 본문

코딩테스트 풀이/JAVA

[LeetCode/Easy] 1281. Subtract the Product and Sum of Digits of an Integer

무지맘 2022. 12. 24. 01:18

1. Input

1) 정수 n

 

2. Output

1) n의 각 자릿수의 곱과 합의 차

 

3. Constraint

1) 1 <= n <= 10^5

 

4. Example

Input: n = 234 -> Output: 15

설명:

- 각 자릿수의 곱 = 2 * 3 * 4 = 24

- 각 자릿수의 합 = 2 + 3 + 4 = 9

- 따라서 24 - 9 = 15를 반환한다.

 

5. Code

1) 첫 코드(2022/06/03)

int pro = 1;
int sum = 0;
int d;
while(n>=1){
    d = n%10;
    pro *= d;
    sum += d;
    n /= 10;
}
return pro-sum;