Algorithm


Problem Name: 2 AD-HOC - beecrowd | 1728

Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1728

Hard to Believe, But True!

 

University of Ulm local Contest DE Germany

Timelimit: 1

The fight goes on, whether to store numbers starting with their most significant digit or their least significant digit. Sometimes this is also called the "Endian War". The battleground dates far back into the early days of computer science. Joe Stoy, in his (by the way excellent) book "Denotational Semantics", tells following story:

"The decision which way round the digits run is, of course, mathematically trivial. Indeed, one early British computer had numbers running from right to left (because the spot on an oscilloscope tube runs from left to right, but in serial logic the least significant digits are dealt with first). Turing used to mystify audiences at public lectures when, quite by accident, he would slip into this mode even for decimal arithmetic, and write things like 73+42=16. The next version of the machine was made more conventional simply by crossing the x-deflection wires: this, however, worried the engineers, whose waveforms were all backwards. That problem was in turn solved by providing a little window so that the engineers (who tended to be behind the computer anyway) could view the oscilloscope screen from the back.
[C. Strachey - private communication.]"

You will play the role of the audience and judge on the truth value of Turing's equations.

 

Input

 

The input contains several test cases. Each specifies on a single line a Turing equation. A Turing equation has the form "a+b=c", where a, b, c are numbers made up of the digits 0,...,9. Each number will consist of at most 7 digits. This includes possible leading or trailing zeros. The equation "0+0=0" will finish the input and has to be processed, too. The equations will not contain spaces.

 

Output

 

For each test case generate a line containing the word "True" or the word "False", if the equation is true or false, respectivelly, in Turing's interpretation, i.e. the numbers being read backwards.

 

 

 

Sample Input Sample Output

73+42=16
5+8=13
10+20=30
0001000+000200=00030
1234+5=1239
1+0=0
7000+8000=51
0+0=0

True
False
True
True
False
False
True
True

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming


#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_set>
#include <vector>
#define mp make_pair
#define pb push_back
#define MAXV 200100
#define PI 3.14159
#define TWOPI 2 * PI

using namespace std;

typedef vector<int> vi;
typedef pair < int, int> ii;
typedef pair<int, ii> iii;
typedef long long int64;

int main()
{
    ios::sync_with_stdio(false);
    string str;
    while (cin >> str) {
        int a = -1, b = -1, c = -1;
        for (int i = str.size() - 1; i >= 0; i--) {
            string s = "";
            while (i >= 0 && isdigit(str[i]))
                s += str[i--];
            if (c == -1) {
                stringstream buffer(s);
                buffer >> c;
            } else if (b == -1) {
                stringstream buffer(s);
                buffer >> b;
            } else if (a == -1) {
                stringstream buffer(s);
                buffer >> a;
            }
        }
        cout << ((a + b == c) ? "True\n" : "False\n");
    }
    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
73+42=16
5+8=13
10+20=30
0001000+000200=00030
1234+5=1239
1+0=0
7000+8000=51
0+0=0

Output

x
+
cmd
True
False
True
True
False
False
True
True

#2 Code Example with Python Programming

Code - Python Programming


while True:
    entrada = input()
    if entrada == "0+0=0":
        print(True)
        break
    numeros = [int(i) for i in entrada[::-1].replace("=", "+").split("+")]
    print(numeros[0] == numeros[2] + numeros[1])
Copy The Code & Try With Live Editor

Input

x
+
cmd
73+42=16
5+8=13
10+20=30
0001000+000200=00030
1234+5=1239
1+0=0
7000+8000=51
0+0=0

Output

x
+
cmd
True
False
True
True
False
False
True
True

#3 Code Example with Java Programming

Code - Java Programming


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class Main {
   static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter out = new PrintWriter(System.out);

    public static void main(String[] args) throws IOException {
        String l;
        String[] P;
        String a, b, c;
        boolean f;
        while (!(l = read()).equals("0+0=0")) {
            P = l.split("\\+");
            a = P[0];
            P = P[1].split("=");
            b = P[0];
            c = P[1];
            a = new StringBuilder(a).reverse().toString();
            b = new StringBuilder(b).reverse().toString();
            c = new StringBuilder(c).reverse().toString();
            f = Integer.parseInt(a) + Integer.parseInt(b) == Integer.parseInt(c);
            out.println(f ? "True" : "False");
        }
        out.println("True");
        out.close();
    }

    private static String read() throws IOException {
        return in.readLine();
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
73+42=16
5+8=13
10+20=30
0001000+000200=00030
1234+5=1239
1+0=0
7000+8000=51
0+0=0

Output

x
+
cmd
True
False
True
True
False
False
True
True
Advertisements

Demonstration


Previous
#1717 Beecrowd Online Judge Solution 1717 Cut Solution in C, C++, Java, Js and Python
Next
#1739 Beecrowd Online Judge Solution 1739 Threebonacci Sequence Solution in C, C++, Java, Js and Python