1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
#include <iostream>
using namespace std;
//function headers
void nextDigit(int oneAns[], int place);
bool repeat(int oneAns[], int place);
bool isSolution(int oneAns[]);
void print(int oneAns[]);
void printArray(int oneAns[]);
const int adjacencies[10][9] = {
{ 0, 7, 8, 9, -1, -1, -1, -1, -1 },
{ 2, 1, 3, 4, 5, 6, -1, -1, -1 },
{ 3, 2, 5, 6, -1, -1, -1, -1, -1 },
{ 4, 1, 2, 5, 8, 7, -1, -1, -1 },
{ 5, 1, 2, 3, 4, 6, 7, 8, 9 },
{ 6, 2, 3, 5, 8, 9, -1, -1, -1 },
{ 7, 4, 5, 8, 0, -1, -1, -1, -1 },
{ 8, 4, 5, 6, 7, 9, 0, -1, -1 },
{ 9, 5, 6, 8, 0, -1, -1, -1, -1 }
};
int main()
{
int oneAns[] = { 3, 7, 1, 2, 4, 5, 9, 8, 0, 6 };
printArray(oneAns);
nextDigit(oneAns, 0);
cin.get();
return EXIT_SUCCESS;
}
bool repeat(int oneAns[], int place)
{
for (int i = 0; i < place-1; i++)
{
if (oneAns[i] == oneAns[place])
{
return true;
}
}
return false;
}
bool isSolution(int oneAns[])
{
int sum = ((oneAns[0] * 100) +(oneAns[1] * 10) + oneAns[2]) +
((oneAns[3] * 100) + (oneAns[4] * 10) + oneAns[5]);
if (sum == ((oneAns[6] * 100) + (oneAns[7] * 10) + oneAns[8]))
{
return true;
}
else
{
return false;
}
}
void print(int oneAns[])
{
cout << ((oneAns[0] * 100) + (oneAns[1] * 10) + oneAns[2]) << " + " <<
((oneAns[3] * 100) + (oneAns[4] * 10) + oneAns[5]) << " = " <<
((oneAns[6] * 100) + (oneAns[7] * 10) + oneAns[8]) << endl;
}
void printArray(int oneAns[])
{
for (int i = 0; i < 9; i++)
{
cout << oneAns[i];
}
cout << endl;
}
void nextDigit(int oneAns[], int place)
{
if (repeat(oneAns, place))
{
exit;
}
else
{
if (isSolution(oneAns))
{
// print out the solution
system("CLS");
print(oneAns);
}
else
{
for (int i = 0; i < 9; i++)
{
oneAns[i] += adjacencies[place][i+1];
printArray(oneAns);
}
printArray(oneAns);
nextDigit(oneAns, place + 1);
}
}
}
|