I've been assigned a school project in which I need to come up with as many integers as possible using only the integers 2 3 4
and the operators + - * / %
. I then have to output the integers with cout
along with how I got that answer. For example:
cout << "2 + 3 - 4 = " << 2 + 3 - 4;
I can only use each integer once per cout
statement, and there can be no duplicate answers.
Everyone else seems to be doing the "brute force" method (i.e., copying and pasting the same statements and changing the numbers and operators), but that hardly seems efficient. I figured I would try cycling through each number and operator one-by-one and checking to see if the answer has already been found, but I'm unsure of what the easiest way to do this would be.
I suppose I could use nested loops, but there's still the problem of checking to see if the answer has already been found. I tried s开发者_开发技巧toring the answers in a vector, but I couldn't pass the vector to a user-defined function that checked to see if a value existed in the vector.
You could use a map
or a hash_map
from the Standard Template Library (STL). These structures store key-value pairs efficiently. Read up on them before you use them but they might give you a good starting point. Hint: The integers you compute would probably make good keys.
Assuming you can use each of the numbers in the set(2, 3, 4) only once there are 3! ways of arranging these 3 numbers. Then there are 2 places for sign and you have total 5 symbols(+ - * / %) so there are 5*5 = 25 ways to do that. So you have total 3! * 25 expressions. Than you can create a hash map where key will be number and value will be the expression. If the hash map contains a key already you skip that expression.
You could try a bit of meta-programming, as follows. It has the advantage of using C itself to calculate the expressions rather than you trying to do your own evaluator (and possibly getting it wrong):
#include <stdlib.h>
#include <iostream>
#include <fstream>
using namespace std;
int main (void) {
int n1, n2, n3;
const char *ops[] = {" + ", " - ", " * ", " / ", " % ", 0};
const char **op1, **op2;
ofstream of;
of.open ("prog2.cpp", ios::out);
of << "#include <iostream>\n";
of << "using namespace std;\n";
of << "#define IXCOUNT 49\n\n";
of << "static int mkIdx (int tot) {\n";
of << " int ix = (IXCOUNT / 2) + tot;\n";
of << " if ((ix >= 0) && (ix < IXCOUNT)) return ix;\n";
of << " cout << \"Need more index space, "
<< "try \" << IXCOUNT + 1 + (ix - IXCOUNT) * 2 << \"\\n\";\n";
of << " return -1;\n";
of << "}\n\n";
of << "int main (void) {\n";
of << " int tot, ix, used[IXCOUNT];\n\n";
of << " for (ix = 0; ix < sizeof(used)/sizeof(*used); ix++)\n";
of << " used[ix] = 0;\n\n";
for (n1 = 2; n1 <= 4; n1++) {
for (n2 = 2; n2 <= 4; n2++) {
if (n2 != n1) {
for (n3 = 2; n3 <= 4; n3++) {
if ((n3 != n1) && (n3 != n2)) {
for (op1 = ops; *op1 != 0; op1++) {
for (op2 = ops; *op2 != 0; op2++) {
of << " tot = " << n1 << *op1 << n2 << *op2 << n3 << ";\n";
of << " if ((ix = mkIdx (tot)) < 0) return ix;\n";
of << " if (!used[ix])\n";
of << " cout << " << n1 << " << \"" << *op1 << "\" << "
<< n2 << " << \"" << *op2 << "\" << " << n3
<< " << \" = \" << tot << \"\\n\";\n";
of << " used[ix] = 1;\n\n";
}
}
}
}
}
}
}
of << " return 0;\n";
of << "}\n";
of.close();
system ("g++ -o prog2 prog2.cpp ; ./prog2");
return 0;
}
This gives you:
2 + 3 + 4 = 9
2 + 3 - 4 = 1
2 + 3 * 4 = 14
2 + 3 / 4 = 2
2 + 3 % 4 = 5
2 - 3 + 4 = 3
2 - 3 - 4 = -5
2 - 3 * 4 = -10
2 - 3 % 4 = -1
2 * 3 + 4 = 10
2 * 3 * 4 = 24
2 / 3 + 4 = 4
2 / 3 - 4 = -4
2 / 3 * 4 = 0
2 % 3 + 4 = 6
2 % 3 - 4 = -2
2 % 3 * 4 = 8
2 * 4 + 3 = 11
2 / 4 - 3 = -3
I'm not entirely certain of the wisdom of handing this in as an assignment however :-)
精彩评论