📜  检查两个整数的不同位数的总和是否相等

📅  最后修改于: 2021-05-06 22:34:13             🧑  作者: Mango

给定两个整数mn ,任务是找到两个数字的不同数字之和,如果两个和相等,则打印YES ,否则打印NO
例子:

方法:找到mn的唯一数字之和,并将它们分别存储在sumMsumN中。如果sumM = sumN,则打印YES,否则打印NO
下面是上述方法的实现:

C++
// C++ program to check if the sum of distinct
// digits of two integers are equal
 
#include 
using namespace std;
 
    // Function to return the sum of
    // distinct digits of a number
     int distinctDigitSum(int n)
    {
        bool used[10];
        int sum = 0;
        while (n > 0) {
 
            // Take last digit
            int digit = n % 10;
 
            // If digit has not been used before
            if (!used[digit]) {
 
                // Set digit as used
                used[digit] = true;
                sum += digit;
            }
 
            // Remove last digit
            n = (int)n / 10;
        }
 
        return sum;
    }
 
    // Function to check whether the sum of
    // distinct digits of two numbers are equal
     string checkSum(int m, int n)
    {
        int sumM = distinctDigitSum(m);
        int sumN = distinctDigitSum(n);
 
        if (sumM != sumN)
            return "YES";
        return "NO";
    }
 
    // Driver code
    int main() {
 
        int m = 2452, n = 9222;
        cout << (checkSum(m, n));
        return 0;
}


Java
// Java program to check if the sum of distinct
// digits of two integers are equal
public class HelloWorld {
 
    // Function to return the sum of
    // distinct digits of a number
    static int distinctDigitSum(int n)
    {
        boolean used[] = new boolean[10];
        int sum = 0;
        while (n > 0) {
 
            // Take last digit
            int digit = n % 10;
 
            // If digit has not been used before
            if (!used[digit]) {
 
                // Set digit as used
                used[digit] = true;
                sum += digit;
            }
 
            // Remove last digit
            n = n / 10;
        }
 
        return sum;
    }
 
    // Function to check whether the sum of
    // distinct digits of two numbers are equal
    static String checkSum(int m, int n)
    {
        int sumM = distinctDigitSum(m);
        int sumN = distinctDigitSum(n);
 
        if (sumM == sumN)
            return "YES";
        return "NO";
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int m = 2452, n = 9222;
        System.out.println(checkSum(m, n));
    }
}


Python3
# Python3 program to check if the sum of
# distinct digits of two integers are equal
 
# Function to return the sum of
# distinct digits of a number
def distinctDigitSum(n) :
     
    used = [False] * 10
    sum = 0
    while (n > 0) :
 
        # Take last digit
        digit = n % 10
         
        # If digit has not been used before
        if (not used[digit]) :
 
            # Set digit as used
            used[digit] = True
            sum += digit
             
        # Remove last digit
        n = n // 10
         
    return sum
     
# Function to check whether the sum of
# distinct digits of two numbers are equal
def checkSum(m, n) :
     
    sumM = distinctDigitSum(m)
    sumN = distinctDigitSum(n)
     
    if (sumM == sumN) :
        return "YES"
    return "NO"
     
# Driver code
if __name__ == "__main__" :
 
    m = 2452
    n = 9222
     
    print(checkSum(m, n))
     
# This code is contributed by Ryuga


C#
// C# program to check if the sum of distinct
// digits of two integers are equal
 
 
// Function to return the sum of
// distinct digits of a number
 
using System;
 
public class GFG{
        static int distinctDigitSum(int n)
    {
        bool []used = new bool[10];
        int sum = 0;
        while (n > 0) {
 
            // Take last digit
            int digit = n % 10;
 
            // If digit has not been used before
            if (!used[digit]) {
 
                // Set digit as used
                used[digit] = true;
                sum += digit;
            }
 
            // Remove last digit
            n = n / 10;
        }
 
        return sum;
    }
 
    // Function to check whether the sum of
    // distinct digits of two numbers are equal
    static String checkSum(int m, int n)
    {
        int sumM = distinctDigitSum(m);
        int sumN = distinctDigitSum(n);
 
        if (sumM == sumN)
            return "YES";
        return "NO";
    }
 
    // Driver code
    static public void Main (){
        int m = 2452, n = 9222;
        Console.WriteLine(checkSum(m, n));
    }
//This code is contributed by akt_mit   
}


PHP
 0)
    {
 
        // Take last digit
        $digit = $n % 10;
 
        // If digit has not been used before
        if ($used > 0)
        {
 
            // Set digit as used
            $used[$digit] = true;
            $sum += $digit;
        }
 
        // Remove last digit
        $n = (int)$n / 10;
    }
 
    return $sum;
}
 
// Function to check whether the sum of
// distinct digits of two numbers are equal
function checkSum($m, $n)
{
    $sumM = distinctDigitSum($m);
    $sumN = distinctDigitSum($n);
     
    if ($sumM != $sumN)
        return "YES";
    return "NO";
}
 
// Driver code
$m = 2452;
$n = 9222;
echo (checkSum($m, $n));
 
// This code is contributed by ajit..
?>


Javascript


输出:
YES