I, entitled 1
Second, the idea
Third, the code
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;
}
}
Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source. ↩︎