c++ - Cryptarithmetic puzzle -


i trying solve cryptarithmetic puzzle 2 + 2 = 4 , used raw brute force, can't figure out making mistake. idea here tries possible combinations of numbers 0 10 , numbers assigned characters must distinct. definition

a cryptarithmetic puzzle mathematical game digits of numbers represented letters (or symbols). each letter represents unique digit. goal find digits such given mathematical equation verified: in case:

   2  + 2  ------  = 4 

this code goes through possible combinations until finds solution satisfies problem. constraint given in else if statement. first if statement checks if numbers same, , if are, skips iteration.

my desired output see all correct solutions displayed.

int t, w, o, f, u, r;  (t = 0; t < 10; t++) {     (w = 0; w < 10; w++)     {         (o = 0; o < 10; o++)         {             (f = 0; f < 10; f++)             {                 (u = 0; u < 10; u++)                 {                     (r = 0; r < 10; r++)                     {                         if ((t == w) || (t == o) || (t == f) || (t == u) || (t == r) || (w == o) || (w == f) || (w == u) || (w == r) || (o == f) || (o == u) || (o == r) || (f == u) || (f == r) || (u == r))                         {                             continue;                         }                         else if (200 * t + 20 * w + 2 * o == f * 1000 + o * 100 + u * 10 + r * 0) {                             cout << "t = " << t << endl                                 << "w = " << w << endl                                 << "o = " << o << endl                                 << "f = " << f << endl                                 << "u = " << u << endl                                 << "r = " << r << endl << endl;                             break;                         }                     }                 }             }         }     } } 

i bunch of results , interestingly enough, last result fine, gives:

t = 7 w = 6 o = 5 f = 1 u = 3 r = 0 

r * 0 should r * 1 .

the answer got happened correct 1 r = 0 (because when r 0, r * 0 same r * 1). guess want start f 1 typically cryptarithms don't allow leading zeros.


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -