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 storing 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
mapor ahash_mapfrom 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.