1281 title, the integer difference between you and the product

I, entitled 1

Here Insert Picture Description

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

  1. 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. ↩︎

Published 48 original articles · won praise 1 · views 862

Guess you like

Origin blog.csdn.net/weixin_45980031/article/details/104117693