Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Cryptarithmetic Problem

$
0
0
I'm given the assignment following this sentence, and I'm pretty sure I did it right however when I run the program I get nothing. You'll see my code as well:

In cryptarithmetic puzzles, mathematical equations are written using letters.
Each letter can be a digit from 0 to 9, but no two letters can be the same.
Customarily, distinct letters stand for
different digits
Here is a sample problem:


SEND + MORE = MONEY

A solution to the puzzle is S = 9, R = 8, O = 0, M = 1, Y = 2, E = 5, N = 6, D
= 7
S E N D
M O R E
+
------------
M O N E Y

That is,

9 5 6 7
1 0 8 5
+
------------
1 0 6 5 2






Write a program that finds solutions to the following cryptarithmetic puzzle:


TOO + TOO + TOO + TOO = GOOD

T O O
T O O
T O O
+
-----------
G O O D


#include<iostream>

using namespace std;

int main()
{
        int t, o, g, d;
        for (t = 0; t <= 9; t++){
                for (o = 0; o <= 9; o++)
                        for (g = 0; g <= 9; g++)
                                for (d = 0; d <= 9; d++){
                                        if ((t != o) && (t != g) && (t != d) && (o != g) && (o != d) && (g != d)){
                                                int too = t * 100 + o * 10 + o, good = g * 1000 + o * 100 + o * 10 + d;
                                                if (4 * good == too){
                                                        cout << "t = " << t << "o = " << o << "g = " << g << "d = "
                                                        << d << endl;
                                                        cout << "TOO = " << too << endl;
                                                        cout << "GOOD = " << good << endl;
                                                }
                                        }
                                }
        }
return 0;
}





Hey, I fixed it, I had the good and the too swapped at the end. It works now, so you can delete this post.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>