美文网首页
1281. Subtract the Product and S

1281. Subtract the Product and S

作者: 30岁每天进步一点点 | 来源:发表于2020-03-01 11:16 被阅读0次

附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;
}

小结:整形数字的处理,用到求余数取每一位,求商、用到循环

相关文章

网友评论

      本文标题:1281. Subtract the Product and S

      本文链接:https://www.haomeiwen.com/subject/tnixkhtx.html