Compruebe si a + b = c es válido después de eliminar todos los ceros de a, b y c

Dados dos enteros a y b . Ahora, c se puede encontrar como a + b = c . La tarea es comprobar si la ecuación sigue siendo válida después de eliminar todos los ceros de a , b y c . Si es válido, imprima ; de lo contrario, imprima No.

Ejemplos: 

Entrada: a = 101, b = 102 
Salida: Sí 
La ecuación actual es 101 + 102 = 203 
Después de eliminar los 0, 11 + 12 = 23 (que sigue siendo correcto)

Entrada: a = 105, b = 106 
Salida: No 
105 + 106 = 211 
15 + 16 = 211 (Incorrecto) 

Acercarse: 

  • Calcula c .
  • Elimina todos los 0 de a , b y c .
  • Compruebe si los nuevos valores forman una ecuación correcta.

A continuación se muestra la implementación del enfoque anterior:

C++

// C++ implementation of the approach
#include<bits/stdc++.h>
#include<string>
#include<iostream>
#include<sstream>
 
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

<?php
// PHP implementation of the approach
 
// Function that returns true if
// the given condition is satisfied
function 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
function remove($x)
{
     
    // Converting x into a string
    $y = (string)$x;
     
    // To store the new integer without 0s
    $num = "";
    for ($i = 0; $i < strlen($y); $i++)
    {
         
        // 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))
    echo "Yes";
else
    echo "No";
 
// This code is contributed by Ryuga
?>

Javascript

<script>
 
// JavaScript implementation of the approach
 
// Function to remove zeroes from a number
function remove(x)
{
    // Converting x into a string
     
    var y = x.toString();
     
    // To store the new integer without 0s
    var num = "";
    var 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 parseInt(num);
}
 
// Function that returns true if
// the given condition is satisfied
function check(a, b)
{
    // Calculate c
    var 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
var a = 101;
var b = 102;
 
if(check(a, b))
    document.write( "Yes");
else
    document.write( "No");
 
</script>
Producción: 

Yes

 

Implementación alternativa en 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

Publicación traducida automáticamente

Artículo escrito por indrajit1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *