24點 計算器

2022-02-01 18:03:06 字數 3999 閱讀 4581

24點計算器

沒別的辦法,只有把各種可能都計算一遍,然後得到能計算出24的表示式,並且盡可能的排除掉重複的表示式

假設有a b c d四個數,操作符用op代替,則有(a op b) op (c op d) 或 ((a op b) op c) op d 兩種形式

程式用c# 2.0開發,支援計算 8 / (3 - 8 / 3) = 24 這種

這個程式最傷腦筋的地方就是如何去掉重複的結果,我自己改寫了gethashcode()方法,以盡可能的讓重複的表示式計算出相同的值出來

syntax:

calc24 [num1 [num2 [num3 [num4]]]]

example:

calc24 3 3 8 8

calc24 3 3 8

calc24 3 3

calc24 3

calc24

若省略引數,則程式自動隨機生成乙個24以內的數代替

code:

#define debug

using system;

using system.collections.generic;

using system.text;

using system.text.regularexpressions;

namespace calculate24

;#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)

}string outstring = calculate.run(v1, v2, v3, v4);

if (!string.isnullorempty(outstring))

else

無論如何都無法計算得出 24rn", new object ));

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

console.readkey();

}private static random random = new random(environment.tickcount);

private static int tryparse(string value)}}

using system;

using system.collections.generic;

using system.text;

namespace calculate24

}if (v2 != v3)

foreach (value vl in listleft)

}if (v2 != v4)

foreach (value vl in listleft)

}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)

foreach (value vl in listright)

}private static void buildvaluelist(valuelist list, double v1, double v2)

else

if (v1 != v2)

else}}

private static void buildvaluelist(valuelist list, value v1, value v2)

else

if (v1.value != v2.value)

else}}

private static void buildvaluelist(valuelist list, value v1, double v2)

else

if (v1.value != v2)

else}}

}}using system;

using system.collections.generic;

using system.text;

namespace calculate24

public value(value v1, value v2, char op)

public value(value v1, double v2, char op)

public value(double v1, value v2, char op)

private void calculatevalue()

calculatehashcode();

}public override string tostring()

else

s1 = v1.tostring();

if (vv2 != null)

else

s2 = v2.tostring();

return string.format(" ", s1, op, s2);

}public override int gethashcode()

private int hashcode = 0;

private void calculatehashcode()

hashcode += buffer.length * 7;}}

}using system;

using system.collections.generic;

using system.text;

namespace calculate24

int hash = item.gethashcode();

foreach (value value in this)

add(item);}}

24點紙牌遊戲計算器

includebool flag 判斷是否有解 function 兩個數之間的計算 param float x 第乙個數 float y 第二個數 int z 運算符號 return float 運算結果 float calculate float x,float y,int z function ...

24點計算器問題 C 實現

24 點遊戲是乙個很有意思的數字遊戲,也是一道常見的演算法面試題。題目是這樣的 任給四個數 為了便於人們心算或口算,一般都是小於 10 的數 對四個數字用各種組合進行加 減 乘 除四則運算,看看結果是否能等於 24?對於面試題來說,這是乙個典型的窮舉型別演算法問題。這個題目比較有意思的地方是它除了要...

實現計算器

dim boldot as boolean dim dblacc1,dblacc2 as double dim dblacc3 as double 10 dim strop as string private sub add num byval intnumber as integer if bol...