📜  从 a、b 和 c 中删除所有零后检查 a + b = c 是否有效

📅  最后修改于: 2022-05-13 01:55:28.371000             🧑  作者: Mango

从 a、b 和 c 中删除所有零后,检查 a + b = c 是否有效

给定两个整数ab 。现在, c可以找到为a + b = c 。任务是检查从abc中删除所有零后等式是否仍然有效。如果有效,则打印Yes否则打印No

例子:

方法:

  • 计算c
  • abc中删除所有0
  • 检查新值是否形成正确的方程式。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include
#include
#include
#include
 
using namespace std;
 
// Function to remove zeroes from a number
int remove(int x)
{
    // Converting x into a string
     
    string y = to_string(x);
     
    // To store the new integer without 0s
    string num;
    int i;
    for(i = 0; i < y.length(); i++)
    {
        // Skip if current character is 0
        if(y[i] == 0)
            continue;
        num += y[i];
    }
         
    // Return the integer after removing 0s
    return stoi(num);
}
 
// Function that returns true if
// the given condition is satisfied
bool check(int a, int b)
{
    // Calculate c
    int c = a + b;
     
    // Remove 0s from a, b and c
    a = remove(a);
    b = remove(b);
    c = remove(c);
     
    // Check if the equation is still correct
    if((a + b) == c)
        return true;
    else
        return false;
}
 
// Driver code
int main()
{
    int a = 101;
    int b = 102;
     
    if(check(a, b))
        cout << "Yes";
    else
        cout << "No";
}
 
// This code is contributed by ita_c


Java
// Java implementation of the approach
import java.util.*;
 
class GFG{
 
// Function to remove zeroes from a number    
public static int remove(int x)
{
     
    // Converting x into a string
    String y = String.valueOf(x);
     
    // To store the new integer without 0s
    String num = "";
    int i;
     
    for(i = 0; i < y.length(); i++)
    {
        
       // Skip if current character is 0
       if(y.charAt(i) == 0)
          continue;
       num += y.charAt(i);
    }
         
    // Return the integer after
    // removing 0s
    return Integer.parseInt(num);
}
 
 
// Function that returns true if
// the given condition is satisfied
public static boolean check(int a, int b)
{
     
    // Calculate c
    int c = a + b;
     
    // Remove 0s from a, b and c
    a = remove(a);
    b = remove(b);
    c = remove(c);
     
    // Check if the equation is
    // still correct
    if((a + b) == c)
        return true;
    else
        return false;
}
 
// Driver code
public static void main(String[] args)
{
    int a = 101;
    int b = 102;
     
    if(check(a, b))
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
 
// This code is contributed by grandmaster


Python3
# Python3 implementation of the approach
 
# Function that returns true if
# the given condition is satisfied
def check(a, b):
     
    # Calculate c
    c = a + b
     
    # Remove 0s from a, b and c
    a = remove(a)
    b = remove(b)
    c = remove(c)
     
    # Check if the equation is still correct
    if((a + b) == c):
        return True
    else:
        return False
 
# Function to remove zeroes from a number
def remove(x):
     
    # Converting x into a string
    y = str(x)
     
    # To store the new integer without 0s
    num = ""
    for i in range(len(y)):
         
        # Skip if current character is 0
        if(y[i] == "0"):
            continue
        num += y[i]
         
    # Return the integer after removing 0s
    return int(num)
 
# Driver code
a = 101
b = 102
 
if(check(a, b)):
    print("Yes")
else:
    print("No")


C#
// C# implementation of the approach
using System;
  
class GFG{
  
// Function to remove zeroes from a number    
public static int remove(int x)
{
     
    // Converting x into a string
    string y = x.ToString();
      
    // To store the new integer without 0s
    string num = "";
    int i;
      
    for(i = 0; i < y.Length; i++)
    {
         
        // Skip if current character is 0
        if (y[i] == 0)
            continue;
             
        num += y[i];
    }
          
    // Return the integer after
    // removing 0s
    return Int32.Parse(num);
}
  
// Function that returns true if
// the given condition is satisfied
public static bool check(int a, int b)
{
     
    // Calculate c
    int c = a + b;
      
    // Remove 0s from a, b and c
    a = remove(a);
    b = remove(b);
    c = remove(c);
      
    // Check if the equation is
    // still correct
    if ((a + b) == c)
        return true;
    else
        return false;
}
  
// Driver code
public static void Main(string[] args)
{
    int a = 101;
    int b = 102;
      
    if (check(a, b))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
 
// This code is contributed by rutvik_56


PHP


Javascript


Python3
# Python3 implementation of the approach
  
# Calculate the sum with Zero's intact
d=a+b
  
# Then convert the numbers to string
# replace '0' with '' (blank)
  
a=str(a).replace('0','')
b=str(b).replace('0','')
  
# Now convert the strings back to numbers
# Add the numbers
c=int(a)+int(b)
  
if c==d:
    print('Yes')
else:
    print('No')
  
# This code is contributed by Sandeep Midde


输出:
Yes

Python中的替代实现:

Python3

# Python3 implementation of the approach
  
# Calculate the sum with Zero's intact
d=a+b
  
# Then convert the numbers to string
# replace '0' with '' (blank)
  
a=str(a).replace('0','')
b=str(b).replace('0','')
  
# Now convert the strings back to numbers
# Add the numbers
c=int(a)+int(b)
  
if c==d:
    print('Yes')
else:
    print('No')
  
# This code is contributed by Sandeep Midde