Note that there are some explanatory texts on larger screens.

plurals
  1. POWriting a C++ version of the algebra game 24
    primarykey
    data
    text
    <p>I am trying to write a C++ program that works like the game 24. For those who don't know how it is played, basically you try to find any way that 4 numbers can total 24 through the four algebraic operators of +, -, /, *, and parenthesis. </p> <p>As an example, say someone inputs 2,3,1,5 ((2+3)*5) - 1 = 24</p> <p>It was relatively simple to code the function to determine if three numbers can make 24 because of the limited number of positions for parenthesis, but I can not figure how code it efficiently when four variables are entered.</p> <hr> <p>I have some permutations working now but I still cannot enumerate all cases because I don't know how to code for the cases where the operations are the same.</p> <p>Also, what is the easiest way to calculate the RPN? I came across many pages such as this one: <a href="http://www.dreamincode.net/forums/index.php?showtopic=15406" rel="nofollow noreferrer">http://www.dreamincode.net/forums/index.php?showtopic=15406</a> but as a beginner, I am not sure how to implement it.</p> <pre><code>#include &lt;iostream&gt; #include &lt;vector&gt; #include &lt;algorithm&gt; using namespace std; bool MakeSum(int num1, int num2, int num3, int num4) { vector&lt;int&gt; vi; vi.push_back(num1); vi.push_back(num2); vi.push_back(num3); vi.push_back(num4); sort(vi.begin(),vi.end()); char a1 = '+'; char a2 = '-'; char a3 = '*'; char a4 = '/'; vector&lt;char&gt; va; va.push_back(a1); va.push_back(a2); va.push_back(a3); va.push_back(a4); sort(va.begin(),va.end()); while(next_permutation(vi.begin(),vi.end())) { while(next_permutation(va.begin(),va.end())) { cout&lt;&lt;vi[0]&lt;&lt;vi[1]&lt;&lt;vi[2]&lt;&lt;vi[3]&lt;&lt; va[0]&lt;&lt;va[1]&lt;&lt;va[2]&lt;&lt;endl; cout&lt;&lt;vi[0]&lt;&lt;vi[1]&lt;&lt;vi[2]&lt;&lt;va[0]&lt;&lt; vi[3]&lt;&lt;va[1]&lt;&lt;va[2]&lt;&lt;endl; cout&lt;&lt;vi[0]&lt;&lt;vi[1]&lt;&lt;vi[2]&lt;&lt;va[0]&lt;&lt; va[1]&lt;&lt;vi[3]&lt;&lt;va[2]&lt;&lt;endl; cout&lt;&lt;vi[0]&lt;&lt;vi[1]&lt;&lt;va[0]&lt;&lt;vi[2]&lt;&lt; vi[3]&lt;&lt;va[1]&lt;&lt;va[2]&lt;&lt;endl; cout&lt;&lt;vi[0]&lt;&lt;vi[1]&lt;&lt;va[0]&lt;&lt;vi[2]&lt;&lt; va[1]&lt;&lt;vi[3]&lt;&lt;va[2]&lt;&lt;endl; } } return 0; } int main() { MakeSum(5,7,2,1); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload