How To Efficiently Check If 10 Variables Are Different
c++, conditional-statements
Solution
You may use the following
return ((1 << W)
| (1 << H)
| (1 << I)
| (1 << T)
| (1 << E)
| (1 << A)
| (1 << R)
| (1 << P)
| (1 << C)
| (1 << N))
== 0x03FF;
Problem
I am trying to solve the following math problem using c++: ``` (Each letter represents an individual digit) WHITE + WATER ------- PICNIC ``` So far I have this code to solve the puzzle: ``` #include <iostream> using namespace std; int main(int argc, char **argv) { for (int w = 5; w < 10; w++) { for (int h = 0; h < 10; h++) { for (int i = 0; i < 10; i++) { for (int t = 0; t < 10; t++) { for (int e = 0; e < 10; e++) { for (int a = 0; a < 10; a++) { for (int r = 0; r < 10; r++) { for (int p = 0; p < 10; p++) { for (int c = 0; c < 10; c++) { for (int n = 0; n < 10; n++) { // I need to check if all the digits are different here if (10000 * w + 1000 * h + 100 * i + 10 * t + e + 10000 * w + 1000 * a + 100 * t + 10 * e + r == 100000 * p + 10000 * i + 1000 * c + 100 * n + 10 * i + c) { cout << "W: " << w << endl; cout << "H: " << h << endl; cout << "I: " << i << endl; cout << "T: " << t << endl; cout << "E: " << e << endl; cout << "A: " << a << endl; cout << "R: " << r << endl; cout << "P: " << p << endl; cout << "C: " << c << endl; cout << "N: " << n << endl; } } } } } } } } } } } } ``` Is there some quick test I can run to check if all of the numbers are different (there are 10 different letters so one must be 0, one must be 1, etc.) EDIT: I tried adding them up and check if equal to 45. I could write another program/method which weeds out the results after the nested loops complete, but is there any better way?