1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Write a program to solve a set of magic formula.

Discussion in 'C++' started by natzuyuki, Sep 8, 2012.

  1. natzuyuki

    natzuyuki New Member

    Sep 8, 2012
    Likes Received:
    Trophy Points:
    Problem Description
    Write a program to solve a set of magic formula.

    A magic formula is obtained by transforming an ordinary formula like 120 + 456 = 576 into ZAD + REC = EFC. The transformation is
    done by setting 1 to Z, 2 to A, 0 to D, 4 to R, 5 to E, 6 to C, and 7 to F.

    Now given a set of magic formula, find the digits represented by the letters appearing in the formula. Note that there will be at most 10 different upper case letters in these formula, the leading letter of all numbers could not be 0, and different letters represent different digits.

    For example, the Z, R, and E could not be 0 in ZAD + REC = EFI, and all Z, A, D, R, E, C, F, and I represent different digits. It is also guaranteed that there will be exactly one set of solution. The magic could be addition (+), subtraction (-), or multiplication (*). The number of magic formula is no more than 3, the number of digits of every number is no more than 5, and all numbers are positive.

    Input Format
    The first line contains a integer N, which is the number of magic formula (ranged from 1 to 3, both inclusive)
    And each of the following N lines contains a magic formula. The format of magic formula is

    lvalue op rvalue = result

    where lvalue, rvalue, and result are strings with upper case letters, and op is the operator (either +, -, or * ).
    Note that they are seperated by exactly one space.

    Output Format
    For each magic formula, your program should output the ordinary formula.
    Each formula a line with the same order of original formula, and seperate each number and operator by exactly one space.

    Sample Input:


    Sample Output:

    9567 + 1085 = 10652

    Sample Input:
    HK - GF = FC

    Sample Output:

    1692 + 1269 = 2961
    212 * 212 = 44944
    87 - 53 = 34

Share This Page