给定两个整数a和b ,任务是将它们相加得到c 。之后,从a , b和c中删除零,并检查a + b = c的修改值,然后返回“ YES”,否则返回“ 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.
方法:
- 创建一个函数以从数字n中删除Zero。
- 检查是否(removeZero(a)+ removeZero(b)== removeZero(a + b))然后打印是,否则打印否
下面是上述方法的实现。
C++
// C++ program to check the sum after
// Removing all zeroes is true or not
#include
using namespace std;
// Function to remove zeroes
int removeZero(int n)
{
// Initialize result to zero holds the
// Result after removing zeroes from no
int res = 0;
// Initialize variable d to 1 that holds
// digits of no
int d = 1;
// Loop while n is greater then zero
while (n > 0) {
// Check if n mod 10 is not equal to
// zero
if (n % 10 != 0) {
// store the result by removing zeroes
// And increment d by 10
res += (n % 10) * d;
d *= 10;
}
// Go to the next digit
n /= 10;
}
// Return the result
return res;
}
// Function to check if sum is true after
// Removing all zeroes.
bool isEqual(int a, int b)
{
// Call removeZero() for both sides
// and check whether they are equal
// After removing zeroes.
if (removeZero(a) + removeZero(b) == removeZero(a + b))
return true;
return false;
}
// Driver code
int main()
{
int a = 105, b = 106;
isEqual(a, b) ? cout << "Yes"
: cout << "No";
return 0;
}
Java
// Java program to check the sum after
// Removing all zeroes is true or not
public class GfG {
// Function to remove zeroes
public static int removeZero(int n)
{
// Initialize result to zero holds the
// Result after removing zeroes from no
int res = 0;
// Initialize variable d to 1 that holds
// digits of no
int d = 1;
// Loop while n is greater then zero
while (n > 0) {
// Check if n mod 10 is not equal to
// zero
if (n % 10 != 0) {
// store the result by removing zeroes
// And increment d by 10
res += (n % 10) * d;
d *= 10;
}
// Go to the next digit
n /= 10;
}
// Return the result
return res;
}
// Function to check if sum is true after
// Removing all zeroes.
public static boolean isEqual(int a, int b)
{
// Call removeZero() for both sides
// and check whether they are equal
// After removing zeroes.
if (removeZero(a) + removeZero(b) == removeZero(a + b))
return true;
return false;
}
public static void main(String []args){
int a = 105, b = 106;
if (isEqual(a, b) == true)
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by Rituraj Jain
Python3
# Python 3 program to check the sum after
# Removing all zeroes is true or not
# Function to remove zeroes
def removeZero(n):
# Initialize result to zero holds the
# Result after removing zeroes from no
res = 0
# Initialize variable d to 1 that
# holds digits of no
d = 1
# Loop while n is greater then zero
while (n > 0):
# Check if n mod 10 is not equal
# to zero
if (n % 10 != 0):
# store the result by removing
# zeroes And increment d by 10
res += (n % 10) * d
d *= 10
# Go to the next digit
n //= 10
# Return the result
return res
# Function to check if sum is true
# after Removing all zeroes.
def isEqual(a, b):
# Call removeZero() for both sides
# and check whether they are equal
# After removing zeroes.
if (removeZero(a) +
removeZero(b) == removeZero(a + b)):
return True
return False
# Driver code
a = 105
b = 106
if(isEqual(a, b)):
print("Yes")
else:
print("No")
# This code is contributed
# by sahishelangia
C#
// C# program to check the sum after
// Removing all zeroes is true or not
using System;
class GFG
{
// Function to remove zeroes
public static int removeZero(int n)
{
// Initialize result to zero holds the
// Result after removing zeroes from no
int res = 0;
// Initialize variable d to 1 that holds
// digits of no
int d = 1;
// Loop while n is greater then zero
while (n > 0)
{
// Check if n mod 10 is not equal to
// zero
if (n % 10 != 0)
{
// store the result by removing zeroes
// And increment d by 10
res += (n % 10) * d;
d *= 10;
}
// Go to the next digit
n /= 10;
}
// Return the result
return res;
}
// Function to check if sum is true after
// Removing all zeroes.
public static bool isEqual(int a, int b)
{
// Call removeZero() for both sides
// and check whether they are equal
// After removing zeroes.
if (removeZero(a) +
removeZero(b) == removeZero(a + b))
return true;
return false;
}
// Driver Code
public static void Main()
{
int a = 105, b = 106;
if (isEqual(a, b) == true)
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
// This code is contributed
// by Akanksha Rai
PHP
0)
{
// Check if n mod 10 is not equal
// to zero
if ($n % 10 != 0)
{
// store the result by removing
// zeroes and increment d by 10
$res += ($n % 10) * $d;
$d *= 10;
}
// Go to the next digit
$n = floor($n / 10);
}
// Return the result
return $res;
}
// Function to check if sum is true
// after Removing all zeroes.
function isEqual($a, $b)
{
// Call removeZero() for both sides
// and check whether they are equal
// After removing zeroes.
if (removeZero($a) +
removeZero($b) == removeZero($a + $b))
return true;
return false;
}
// Driver code
$a = 105 ;
$b = 106 ;
if (isEqual($a, $b))
echo "Yes";
else
echo "No";
// This code is contributed by Ryuga
?>
输出:
No