题1281、整数的各位积和之差
程序员文章站
2022-07-15 11:16:12
...
一、题目1
二、思路
三、代码
public class T1281 {
public static void main(String[] args) {
System.out.println( subtractProductAndSum(234)); //15
System.out.println( subtractProductAndSum(4)); //0
System.out.println( subtractProductAndSum(23)); //1
System.out.println( subtractProductAndSum(0)); //0
System.out.println( subtractProductAndSum(705)); //-12
System.out.println( subtractProductAndSum(70)); //-7
}
public static int subtractProductAndSum(int n) {
if ( n == 0 )
return 0;
int sum = 0;
int acc = 1;
int num = n;
int next = num%10;
//System.out.println( "next:"+next + "\t sum:" + sum + "\t acc:" + acc);
while ( num != 0 ){
//System.out.println( "next:"+next + "\t sum:" + sum + "\t acc:" + acc);
sum += next;
acc *= next;
num = num/10;
next = num%10;
}
return acc - sum;
}
}
-
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 ↩︎
上一篇: 提权
下一篇: Spring 的 Maven 依赖
推荐阅读