Given the format string (11 characters, without space) in the form of ABC+DEF=GHI, where A, B, C, D, E, F, G, H, I are representing decimal digit (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) "placeholders" (not necessarily distinct!), develop and implement an algorithm to find the 3-digit positive numbers (ABC, DEF, and GHI) that give maximum GHI according to the equality given. The letters (A, B, C, D, E, F, G, H, I) are "placeholders", so you may have repeating digits. For example in the formula ABA+BBB=GGB, all B’s represent same decimal digit. Your algorithm should also check if such numbers are not possible to find. For example, for the formula AAB+AAB=AAB, it is not possible to satisfy it with decimal digits. Your program will tell "No solution!".There might be more than one solution. In this case just output the first one your algorithm finds. Continue till the user enter -1 for the formula for which your program will exit by saying "Bye!".
Here is a sample run:
Enter the formula:AAB+AAB=AAB
No solution! Enter the formula:AAA+BBB=AAA
999+000=999
Enter the formula: -1
Bye!
Here is the code I've written before:
#include<iostream>
#include<cmath>
#include<string>
using namespace std;
int main()
{
string str = "abcdefghi";
string num = "0123456789";
string formula;
int var1[3];
int var2[3];
int var3[3];
while (1)
{
cout << "Enter the formula: ";
cin >> formula;
if (formula == "-1")
{
cout << "Bye" << endl;
break;
}
else
{
for (int i = 0; i < str.length(); i++)
{
if (formula.at(0) == str.at(i))
{
var1[0] = i;
}
if (formula.at(1) == str.at(i))
{
var1[1] = i;
}
if (formula.at(2) == str.at(i))
{
var1[2] = i;
}
}
for (int i = 0; i < str.length(); i++)
{
if (formula.at(4) == str.at(i))
{
var2[0] = i;
}
if (formula.at(5) == str.at(i))
{
var2[1] = i;
}
if (formula.at(6) == str.at(i))
{
var2[2] = i;
}
}
for (int i = 0; i < str.length(); i++)
{
if (formula.at(8) == str.at(i))
{
var3[0] = i;
}
if (formula.at(9) == str.at(i))
{
var3[1] = i;
}
if (formula.at(10) == str.at(i))
{
var3[2] = i;
}
}
if (var1[0] + var2[0] == var3[0] && var1[1] + var2[1] == var3[1] && var1[2] + var2[2] == var3[2])
cout << var1[0] << var1[1] << var1[2] << "+" << var2[0] << var2[1] << var2[2] << "=" << var3[0] << var3[1] << var3[2] << endl << endl;
else
cout << "No Solution!" << endl << endl;
}
}
system("pause");
return 0;
}
But it doesn't seem to solve the problem exactly. How can I modify my code to get what this question wants.
The answer can be found by brute force. Assume you have an array or vector of 10 digits. With 10 possible digit values there are 10! = 3628800 possible permutations. Of each permutation you can assign the first 9 elements to each of the 9 possible letters check if that provides a new max value.
If your letter values are in
int letter_vals[10];for example then you populate the initial values with:Then you can iterate through the possible letter assignments:
A string can be translated to a value through the permutation e.g.
If different letters may represent the same digit then you could also have applied brute force, but then you would have 10 possible values per letter and 9 letters for a total number of 10^9 possible combinations. Perhaps something like:
The answers differ depending on whether or not repeating digits are allowed. Without repeating digits the program is much faster, with fewer answers: Evaluating: AAC+BBF=GHI
With repeating digits there are more possible combinations: