Check whether a + b = c or not after removing all zeroes from a,b and c

Check whether a + b = c or not after removing all zeroes from a,b and c

Given two integers a and b, the task is to add them to get c. After that remove zeroes from a, b and c and check for modified values if a + b = c then return “YES” else return “NO”.

Input: a = 101, b = 102
Output: YES
101 + 102 = 203.
After removing all zeroes from a, b and c, a = 11, b = 12 and c = 23
Now check if a + b = c i.e. 11 + 12 = 23 . So print Yes.

Input: a = 105, b = 108
Output: NO
After removing all zeroes a + b!= c, therefore the output is NO.

/**
 * SolutionRemoveZero class
 * https://www.geeksforgeeks.org/check-whether-a-b-c-or-not-after-removing-all-zeroes-from-ab-and-c/
 * @author libin
 * @date 2019/1/25 10:57
 */
public class SolutionRemoveZero {

    public static int removeZero(int n) {
        int res = 0;
        int d = 1;
        while (n > 0) {
            if (n % 10 != 0) {
                res += (n % 10) * d;
                d *= 10;
            }
            n /= 10;
        }
        return res;
    }

    public static boolean isEqual(int a, int b) {
        if (removeZero(a) + removeZero(b) == removeZero(a + b)) {
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        int a = 101, b = 102;
        if (isEqual(a, b) == true) {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    }
}

更多请看:https://www.geeksforgeeks.org/check-whether-a-b-c-or-not-after-removing-all-zeroes-from-ab-and-c/

原文地址:https://www.cnblogs.com/hgnulb/p/10318560.html