1281. Subtract the Product and Sum of Digits of an Integer

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

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。