Dont know how to replace each letter as s number can someone help me ... this is the question i was given ...
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. 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.
Write a program that finds a solution to the cryptarithmetic puzzle of the following:
TOO + TOO + TOO + TOO = GOOD
The simplest technique is to use a nested loop for each unique letter (in this case T, O, G, D). The loops would systematically assign the digits from 0 to 9 to each letter. For example, it might first try T=0,O=0,G=0,D=0, thenT=0,O=0, G = 0, D = 1, then T = 0, O = 0, G = 0, D = 2, etc., up to T = 9, O = 9, G = 9, D = 9. In the loop body, test that each variable is unique and that the equation is satisfied. Output the values for the letters that satisfy the equation.
if(!(T == O || T == G || T == D || O == G || O == D || G == D))...
or declare a vector of exclusions that you push_back() with the value of each for loop and have another loop in each loop that checks if the value is in the exclusion vector, but I prefer the first technique... :-)
[edit] In fact, you need to initialise an exclusion vector like std::vector<unsignedint> exclusions = {0, 9, 8, 1, 2, 5, 6, 7}
[/edit]
Thanks condor ... I thought there was a specific function i needed to replace each letter with a number but what you show there makes sense as in putting it in its slots in numerical value and adding it together ... Appreciate the help.
So I think I got it up until repeating numbers ... I can't figure out how to make it so it doesnt finish the loop if one of the numbers are repeated ... Any ideas?
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
int T=0,O=0,G=0,D=0, answer=0, good=1;
while (answer != good && T < 9)
{
O = 0;
T++;
answer = 4 * (T * 100 + O * 10 + O);
good = G * 1000 + O * 100 + O * 10 + D;
printf("second loop %i .... %i\n", answer, good);
while (answer != good && O < 9)
{
G = 0;
O++;
answer = 4 * (T * 100 + O * 10 + O);
good = G * 1000 + O * 100 + O * 10 + D;
printf("third loop %i .... %i\n", answer, good);
while (answer != good && G < 9)
{
D = 0;
G++;
answer = 4 * (T * 100 + O * 10 + O);
good = G * 1000 + O * 100 + O * 10 + D;
printf("fourth loop %i .... %i\n", answer, good);
while (answer != good && D < 9)
{
D++;
answer = 4 * (T * 100 + O * 10 + O);
good = G * 1000 + O * 100 + O * 10 + D;
I found that there are 24 solutions to this puzzle, and none of them are Condor's...
@Condor Where do you get this 1000*, 100* stuff, how's it supposed to solve the puzzle?
Me, I applied the nested loop previously discussed, and just verified that the values were different from each other AND that the equation was satisfied...
it comes up to 24 solutions
[edit] @Condor ok... I get it... the letters become actual numbers... right... damn! I'll try again...
#include <iostream>
usingnamespace std;
int main()
{
for(int t = 1; t < 10; ++t)//t can't be 0 (it is the MSD = Most Significant Digit)!
for(int o = 0; o < 10; ++o)
for(int g = 1; g < 10; ++g)//also g can't be 0 ! (MSD)
for(int d = 0; d < 10; ++d)
if((1000 * g - 400 * t + 66 * o + d) == 0)//if equation is satisfied
if(g != t && g != o && g != d && t != o && t != d && o != d)//different each other
{
cout << "\nSolution" << ": T = " << t << ", O = " << o <<
", G = " << g << ", D = " << d << "\n\n";
cout << "Check: ";
cout << " TOO + TOO + TOO + TOO = GOOD" << '\n';
cout << "\t" << t << o << o << " + " << t << o << o <<
" + " << t << o << o << " + " <<t << o << o << " = " <<
g << o << o << d << '\n';
}
return 0;
}
Output:
Solution: T = 4, O = 9, G = 1, D = 6
Check: TOO + TOO + TOO + TOO = GOOD
499 + 499 + 499 + 499 = 1996
Thanks a lot guys awesome help ... definitely showed me a lot of tricks to narrow down my programming as well ... Thanks for the help @Condor and @@AeonFlux1212