附leetcode链接:https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/
1281. Subtract the Product and Sum of Digits of an Integer
Given an integer number n, return the difference between the product of its digits and the sum of its digits.
public int subtactProductAndSum(int n) {
int product = 1;
int sum = 0;
while(n>0) {
product *= n%10;
sum += n%10;
n = n/10;
}
return product-sum;
}
小结:整形数字的处理,用到求余数取每一位,求商、用到循环
网友评论