本文主要是介绍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.
Example 1:
Input: n = 234 Output: 15 Explanation: Product of digits = 2 * 3 * 4 = 24 Sum of digits = 2 + 3 + 4 = 9 Result = 24 - 9 = 15
思路:取最后一位 % 10;
class Solution {public int subtractProductAndSum(int n) {return (product(n) - sum(n));}private int product(int n) {int res = 1;while(n != 0) {int num = n % 10;n = n / 10;res *= num;}return res;} private int sum(int n) {int res = 0;while(n != 0) {int num = n % 10;n = n / 10;res += num;}return res;}
}
这篇关于Subtract the Product and Sum of Digits of an Integer的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!