24-point Calculator

Source: Internet
Author: User

24-point Calculator

There is no other way. You only need to calculate all kinds of possibilities, and then get an expression that can calculate 24, and eliminate repeated expressions as much as possible.

Assume that there are four numbers of a B c d, and the operator is replaced by OP, there are (a op B) OP (c op D) or (a op B) op C) OP d

ProgramDeveloped in C #2.0 and supports computing 8/(3-8/3) = 24

The biggest headache of this program is how to get rid of repeated results. I have rewritten the gethashcode () method to make repeated expressions calculate the same value as much as possible.

Syntax:

Calc24 [num1 [num2 [num3 [num4]

Example:

Calc24 3 3 8 8
Calc24 3 3 8
Calc24 3 3
Calc24 3
Calc24

If the parameter is omitted, the program automatically generates a random number of less than 24

Code:

========================================================== ========================================================== ==================

# Define debug
Using system;
Using system. Collections. Generic;
Using system. text;
Using system. Text. regularexpressions;

Namespace calculate24
{
Class Program
{
Static void main (string [] ARGs)
{
# If debug
ARGs = new string [] {"2", "2", "2", "4 "};
# Endif
String [] list = new string [] {"", ""};

Int V1 = 0;
Int v2 = 0;
Int V3 = 0;
Int V4 = 0;

Int I = 0;

Console. writeline ("24 point calculator RN ");

If (ARGs. length> 0)
Array. Copy (ARGs, list, math. Min (ARGs. length, list. Length ));

Foreach (string item in List)
{
Switch (I)
{
Case 0:
V1 = tryparse (item );
I ++;
Break;
Case 1:
V2 = tryparse (item );
I ++;
Break;
Case 2:
V3 = tryparse (item );
I ++;
Break;
Case 3:
V4 = tryparse (item );
I ++;
Break;
}
}

String outstring = calculate. Run (V1, V2, V3, V4 );

If (! String. isnullorempty (outstring ))
{
Console. writeline (outstring );
}
Else
{
Console. writeline (string. format ("{0} {1} {2} {3} in any case, 24rn cannot be calculated.", new object [] {V1, V2, V3, V4 }));
}

Console. writeline ("press any key to continue ...");
Console. readkey ();
}

Private Static random = new random (environment. tickcount );
Private Static int tryparse (string value)
{
Int result = 0;

If (value. length> 0)
Int. tryparse (value, out result );

If (result = random. next (1, 24); // throw new exception ("the input number must be greater than 0. The input value [" + value + "] is found. ");

Return result;
}
}
}

========================================================== ========================================================== ==================

Using system;
Using system. Collections. Generic;
Using system. text;

Namespace calculate24
{
Public class calculate
{
Public static string run (INT V1, int V2, int V3, int V4)
{
ValueList listleft = new ValueList ();
ValueList listright = new ValueList ();
ValueList listresult = new ValueList ();

Buildvaluelist (listleft, V1, V2 );
Buildvaluelist (listright, V3, V4 );

Foreach (value Vl in listleft)
{
Foreach (value VR in listright)
{
Buildvaluelist (listresult, VL, VR );
}
}

If (V2! = V3)
{
Buildvaluelist (listleft, V1, V3 );
Buildvaluelist (listright, V2, V4 );
}
Foreach (value Vl in listleft)
{
Foreach (value VR in listright)
{
Buildvaluelist (listresult, VL, VR );
}
}

If (V2! = V4)
{
Buildvaluelist (listleft, V1, V4 );
Buildvaluelist (listright, V2, V3 );
}
Foreach (value Vl in listleft)
{
Foreach (value VR in listright)
{
Buildvaluelist (listresult, VL, VR );
}
}

Buildvaluelist (listleft, listright, listresult, V1, V2, V3, V4 );
Buildvaluelist (listleft, listright, listresult, V1, V3, V2, V4 );
Buildvaluelist (listleft, listright, listresult, V1, V3, V4, V2 );
Buildvaluelist (listleft, listright, listresult, V1, V2, V4, V3 );
Buildvaluelist (listleft, listright, listresult, V1, V4, V2, V3 );
Buildvaluelist (listleft, listright, listresult, V1, V4, V3, V2 );

Buildvaluelist (listleft, listright, listresult, V2, V3, V1, V4 );
Buildvaluelist (listleft, listright, listresult, V2, V3, V4, V1 );
Buildvaluelist (listleft, listright, listresult, V2, V4, V1, V3 );
Buildvaluelist (listleft, listright, listresult, V2, V4, V3, V1 );

Buildvaluelist (listleft, listright, listresult, V3, V4, V1, V2 );
Buildvaluelist (listleft, listright, listresult, V3, V4, V2, V1 );

Return listresult. tostring ();
}

Private Static void buildvaluelist (ValueList listleft, ValueList listright, ValueList listresult,
Double V1, double V2, double V3, double V4)
{
Buildvaluelist (listleft, V1, V2 );
Listright. Clear ();
Foreach (value Vl in listleft)
{
Buildvaluelist (listright, VL, V3 );
}
Foreach (value Vl in listright)
{
Buildvaluelist (listresult, VL, V4 );
}
}

Private Static void buildvaluelist (ValueList list, double V1, double V2)
{< br> list. clear ();
list. addvalue (new value (V1, V2, '+');
list. addvalue (new value (V1, V2, '*');
If (V1> = V2)
{< br> list. addvalue (new value (V1, V2, '-');
list. addvalue (new value (V1, V2, '/');
}< br> else
{< br> list. addvalue (new value (V2, V1, '-');
list. addvalue (new value (V2, V1 ,'/') );
}< br> If (V1! = V2)
{< br> If (V1> = V2)
{< br> list. addvalue (new value (V2, V1, '-');
list. addvalue (new value (V2, V1, '/');
}< br> else
{< br> list. addvalue (new value (V1, V2, '-');
list. addvalue (new value (V1, V2, '/');
}< BR >}

Private Static void buildvaluelist (ValueList list, value V1, value V2)
{< br> list. addvalue (new value (V1, V2, '+');
list. addvalue (new value (V1, V2, '*');
If (v1.value> = v2.value)
{< br> list. addvalue (new value (V1, V2, '-');
list. addvalue (new value (V1, V2, '/');
}< br> else
{< br> list. addvalue (new value (V2, V1, '-');
list. addvalue (new value (V2, V1, '/');
}
If (v1.value! = V2.value)
{< br> If (v1.value> = v2.value)
{< br> list. addvalue (new value (V2, V1, '-');
list. addvalue (new value (V2, V1, '/');
}< br> else
{< br> list. addvalue (new value (V1, V2, '-');
list. addvalue (new value (V1, V2, '/');
}< BR >}

Private Static void buildvaluelist (ValueList list, value V1, double V2)
{
List. addvalue (new value (V1, V2, '+ '));
List. addvalue (new value (V1, V2 ,'*'));
If (v1.value> = V2)
{
List. addvalue (new value (V1, V2 ,'-'));
List. addvalue (new value (V1, V2 ,'/'));
}
Else
{
List. addvalue (new value (V2, V1 ,'-'));
List. addvalue (new value (V2, V1 ,'/'));
}
If (v1.value! = V2)
{
If (v1.value> = V2)
{
List. addvalue (new value (V2, V1 ,'-'));
List. addvalue (new value (V2, V1 ,'/'));
}
Else
{
List. addvalue (new value (V1, V2 ,'-'));
List. addvalue (new value (V1, V2 ,'/'));
}
}
}
}
}

========================================================== ========================================================== ==================

Using system;
Using system. Collections. Generic;
Using system. text;

Namespace calculate24
{
Public class value
{

Private double V1;
Private double V2;
Private char op;

Public double value;

Public Value vv1 = NULL;
Public Value vv2 = NULL;

Public value (double V1, double V2, char OP)
{
V1 = V1;
V2 = V2;
OP = op;
Calculatevalue ();
}

Public value (value V1, value V2, char OP)
{
V1 = v1.value;
Vv1 = V1;
V2 = v2.value;
Vv2 = V2;
OP = op;
Calculatevalue ();
}

Public value (value V1, double V2, char OP)
{
V1 = v1.value;
Vv1 = V1;
V2 = V2;
OP = op;
Calculatevalue ();
}

Public value (double V1, value V2, char OP)
{
V1 = V1;
V2 = v2.value;
Vv2 = V2;
OP = op;
Calculatevalue ();
}

Private void calculatevalue ()
{
Value = 0;

Switch (OP)
{
Case '+ ':
Value = V1 + V2;
Break;
Case '-':
Value = V1-V2;
Break;
Case '*':
Value = V1 * V2;
Break;
Case '/':
If (V2! = 0)
Value = V1/V2;
Else
Value = double. minvalue;
Break;
}
Calculatehashcode ();
}

Public override string tostring ()
{
String S1;
String S2;
If (vv1! = NULL)
{
S1 = vv1.tostring ();
If ("*/". indexof (OP)> = 0 & "+-". indexof (vv1.op)> = 0)
S1 = "(" + S1 + ")";
}
Else
S1 = v1.tostring ();
If (vv2! = NULL)
{
S2 = vv2.tostring ();
If (("*/". indexof (OP)> = 0 & "+ -". indexof (vv2.op)> = 0) | (OP = '-' & "+ -". indexof (vv2.op)> = 0) | (OP = '/'&&"*/". indexof (vv2.op)> = 0 ))
S2 = "(" + S2 + ")";
}
Else
S2 = v2.tostring ();
Return string. Format ("{0} {1} {2}", S1, op, S2 );
}

Public override int gethashcode ()
{
Return hashcode;
}

Private int hashcode = 0;

Private void calculatehashcode ()
{
Hashcode = value. gethashcode ();

Byte [] buffer = encoding. utf8.getbytes (tostring ());

Foreach (byte B in buffer)
{
Hashcode + = B;
}

Hashcode + = buffer. length * 7;
}
}
}

========================================================== ========================================================== ==================

Using system;
Using system. Collections. Generic;
Using system. text;

namespace calculate24
{< br> public class ValueList: List
{
Public void addvalue (value item)
{
If (item. value! = Double. minvalue)
{
bool isresult = (item. vv1! = NULL & item. vv2! = NULL) | (item. vv1! = NULL & (item. vv1.vv1! = NULL |
item. vv1.vv2! = NULL) | (item. vv2! = NULL & (item. vv2.vv1! = NULL | item. vv2.vv2! = NULL )));
If (isresult & math. Abs (item. Value-24)> 0.00001)
{
// console. writeline ("Debug:" + item. tostring ());
return;
}
int hash = item. gethashcode ();
foreach (value in this)
{
If (hash = value. gethashcode ())
return;
}
Add (item );
}
}

Public override string tostring ()
{
Stringbuilder sb = new stringbuilder ();
Foreach (value item in this)
{
SB. appendline (item. tostring () + "= 24 ");
}
Return sb. tostring ();
}
}
}

========================================================== ========================================================== ==================
Appendix:

The following are some of the only answers. You can try:

1 1 3 4
1 3 4 6
1 3 9 10
1 3 13 16
1 4 7 9
1 5 5 5
1 5 7 9
1 5 8 13
1 5 9 9
1 5 11 16
1 6 7 9
1 7 7 9
1 7 7 12
1 7 7 17
1 9 11 11
1 9 11 13
2 2 5 8
2 2 11 11
2 3 10 10
2 4 7 7
2 4 10 10
2 5 5 10
2 5 6 9
2 5 7 9
2 16 18 19
3 3 3 3
3 3 7 7
3 3 8 8
3 3 8 201
3 3 8 208
3 3 23 207
3 5 6 77
4 4 7 7
4 4 10 10
4 5 6 9
4 16 18 19
5 5 18 77
5 10 10 13
5 10 10 17
6 6 8 8
7 8 9 10
7 8 9 12
7 8 10 10
7 8 10 11
9 9 9 12
9 11 12 13
11 13 17 19

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.