public class ershidian {
成都創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比商都網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式商都網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋商都地區(qū)。費用合理售后完善,十年實體公司更值得信賴。
// int h=0;
// public void aaa(int a[],int c)
// {
// int i=0;
// if(c==0){
// h+= a[i]+a[i+1];
// }else if(c==1){
// h+= a[i]-a[i+1];
// }else if(c==2){
// h+= a[i]*a[i+1];
// }else{
// h+= a[i]/a[i+1];
// }
// i++;
// System.out.println(h);
// }
public void kaka(int a[]){
int hi[]=new int[4];
int hj[]=new int[16];
int hf[]=new int[64];
// int hm[]=new int[4];
// int hm2[]=new int[4];
// int hv[]=new int [16];
int z=0;
int n=0;
// int p=0;
for(int i=0;i4;i++){
if(i==0){
hi[i]=a[0]+a[1];
}else if(i==1){
hi[i]=a[0]-a[1];
}else if(i==2){
hi[i]=a[0]*a[1];
}else if(i==3){
hi[i]=a[0]/a[1];
}
}
for(int j=0;j4;j++){
for(int g=0;g4;g++){
if(g==0){
hj[z]=hi[j]+a[2];
}else if(g==1){
hj[z]=hi[j]-a[2];
}else if(g==2){
hj[z]=hi[j]*a[2];
}else if(g==3){
hj[z]=hi[j]/a[2];
}
z++;
}
}
for(int f=0;f16;f++){
for(int r=0;r4;r++){
if(r==0){
hf[n]=hj[f]+a[3];
}else if(r==1){
hf[n]=hj[f]-a[3];
}else if(r==2){
hf[n]=hj[f]*a[3];
}else if(r==3){
hf[n]=hj[f]/a[3];
}
n++;
}
}
// for(int m=0;m4;m++){
// if(m==0){
// hm[m]=a[0]+a[1];
// hm2[m]=a[2]+a[3];
// }else if(m==1){
// hm[m]=a[0]-a[1];
// hm2[m]=a[2]-a[3];
// }else if(m==2){
// hm[m]=a[0]*a[1];
// hm2[m]=a[2]*a[3];
// }else if(m==3){
// hm[m]=a[0]/a[1];
// hm2[m]=a[2]/a[3];
// }
//
// }
// for(int v=0;v4;v++){
// for(int l=0;l4;l++){
// if(l==0){
// hv[p]=hm[v]+hm2[v];
// }else if(l==1){
// hv[p]=hm[v]-hm2[v];
// }else if(l==2){
// hv[p]=hm[v]*hm2[v];
// }else if(l==3){
// hv[p]=hm[v]/hm2[v];
// }
// System.out.println(hv[p]);
// p++;
//
// }
//
// }
for(int k=0;khf.length;k++){
if(hf[k]==24){
System.out.println(hf[k]);
}
}
}
public static void main(String args[]){
int aa[]={2,5,9,9};
ershidian y=new ershidian();
y.kaka(aa);
}
}
這只能算部分的,全部的還做不出
import java.util.Scanner;
/** 給定4個數(shù)字計算24 */
public class Core {
private double expressionResult = 24;
// private int maxLine=10;
private boolean error = true;
private double numbers[] = new double[4];
public Object resultReturn;
/**
* 該對象擁有3個私有變量 expressionResult,所需結(jié)果 maxLine,輸出結(jié)果每頁行數(shù) error,是否出錯
* numbers[4],記錄用來運算的4個數(shù)
*
* 其次,該對象擁有以下方法供外部調(diào)用 setNumbers(double[] 運算的數(shù)) 輸入用來運算的數(shù),4個時才能計算,無返回
* setMaxLine(int 行數(shù)) 輸入每頁的行數(shù),無返回 getMaxLine() 返回每頁的行數(shù),類型為int
* setExpressionResult(double 所需結(jié)果) 輸入所需結(jié)果,無返回 getExpressionResult()
* 返回所需結(jié)果,類型為double getExpression() 返回可得出所需結(jié)果的表達式,類型為字符串數(shù)組
*
* 最后,私有方法均為計算與表達式轉(zhuǎn)換部分
*/
// 測試使用
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] arr = new int[4];
System.out.print("輸入第一個數(shù):");
arr[0] = scanner.nextInt();
System.out.print("輸入第二個數(shù):");
arr[1] = scanner.nextInt();
System.out.print("輸入第三個數(shù):");
arr[2] = scanner.nextInt();
System.out.print("輸入第四個數(shù):");
arr[3] = scanner.nextInt();
Core s = new Core();
s.setNumbers(arr);
String[] output = s.getExpression();
for (int i = 0; i output.length; i++) {
System.out.println(output[i]);
}
}
/** 設(shè)定被計算的四個數(shù),由于是數(shù)組,所以具有容錯功能(不為4個數(shù)) */
public void setNumbers(double[] n) {
if (n.length == 4) {
error = false;
numbers = n;
} else
error = true;
}
public void setNumbers(int[] n) {
if (n.length == 4) {
error = false;
for (int i = 0; i 4; i++) {
numbers[i] = n[i];
}
} else
error = true;
}
/** 設(shè)定每頁顯示的行數(shù) */
// public void setMaxLine(int n) {
// if (n0) {
// maxLine=n;
// }
// }
// /** 返回每頁顯示的行數(shù) */
// public int getMaxLine() {
// return maxLine;
// }
/** 設(shè)定需要得到的結(jié)果 */
public void setExpressionResult(double n) {
expressionResult = n;
}
/** 返回所需結(jié)果 */
public double expressionResult() {
return expressionResult;
}
/** 返回符合條件的表達式 */
public String[] getExpression() {
if (!error) {
String[] expression = calculate(numbers);
return expression;
} else
return new String[] { "出錯了,輸入有誤" };
}
/** cal24(),輸出結(jié)果為24的表達式 */
private String[] calculate(double[] n) {
if (n.length != 4)
return new String[] { "Error" };
double[] n1 = new double[3];
double[] n2 = new double[2];
String[] resultString = new String[1024]; // 最多1000組解,暫時未溢出
int count = 0;
boolean isRepeat = false;
for (int t1 = 0; t1 6; t1++) {
for (int c1 = 0; c1 6; c1++) {
for (int t2 = 0; t2 3; t2++) {
for (int c2 = 0; c2 6; c2++) {
for (int c3 = 0; c3 6; c3++) {
if ((c1 / 3 == c2 / 3 (c1 % 3) * (c2 % 3) != 0)
|| (c2 / 3 == c3 / 3 (c2 % 3) * (c3 % 3) != 0)
|| (c1 / 3 == c3 / 3
(c1 % 3) * (c3 % 3) != 0 t2 == 2)) {
// 去除連減連除的解,因為x/(y/z)=x*z/y
continue;
}
n1 = cal1(n, t1, c1);
n2 = cal2(n1, t2, c2);
double result = cal(n2[0], n2[1], c3);
if ((result - expressionResult) 0.00000001
(expressionResult - result) 0.00000001) {
resultString[count] = calString(n, t1, c1, t2,
c2, c3)
+ "=" + (int) expressionResult;
for (int i = 0; i count; i++) {
isRepeat = false;
if (resultString[i]
.equals(resultString[count])) { // 去除完全重復(fù)的解
isRepeat = true;
break; // 提前退出循環(huán)
}
}
if (c1 == c2 c2 == c3 c1 % 3 == 0
t1 + t2 != 0) { // 連加連乘
isRepeat = true;
}
if (!isRepeat) {
count++;
}
}
}
}
}
}
}
if (count == 0)
return new String[] { "該組數(shù)無解" };
String[] resultReturn = new String[count];
System.arraycopy(resultString, 0, resultReturn, 0, count);
return resultReturn;
}
/** cal1(),將4個數(shù)計算一次后返回3個數(shù) */
private double[] cal1(double[] n, int t, int c) { // t為原來的t1,c為原來的c1
double[] m = new double[3];
switch (t) {
case 0:
m[1] = n[2];
m[2] = n[3];
m[0] = cal(n[0], n[1], c);
break;
case 1:
m[1] = n[1];
m[2] = n[3];
m[0] = cal(n[0], n[2], c);
break;
case 2:
m[1] = n[1];
m[2] = n[2];
m[0] = cal(n[0], n[3], c);
break;
case 3:
m[1] = n[0];
m[2] = n[3];
m[0] = cal(n[1], n[2], c);
break;
case 4:
m[1] = n[0];
m[2] = n[2];
m[0] = cal(n[1], n[3], c);
break;
default:
m[1] = n[0];
m[2] = n[1];
m[0] = cal(n[2], n[3], c);
}
return m;
}
/** cal2(),將3個數(shù)計算一次后返回2個數(shù) */
private double[] cal2(double[] n, int t, int c) { // t為原來的t2,c為原來的c2
double[] m = new double[2];
switch (t) {
case 0:
m[1] = n[2];
m[0] = cal(n[0], n[1], c);
break;
case 1:
m[1] = n[1];
m[0] = cal(n[0], n[2], c);
break;
default:
m[1] = n[0];
m[0] = cal(n[1], n[2], c);
}
return m;
}
/** cal(),將2個數(shù)計算后返回結(jié)果 */
private double cal(double n1, double n2, int c) { // n1,n2為運算數(shù),c為運算類型
switch (c) {
case 0:
return n1 + n2;
case 1:
return n1 - n2;
case 2:
return n2 - n1;
case 3:
return n1 * n2;
case 4:
if (n2 == 0)
return 9999; // 使計算結(jié)果必不為24
else
return n1 / n2;
default:
if (n1 == 0)
return 9999; // 同上
else
return n2 / n1;
}
}
/** calString(),輸出表達式 */
private String calString(double[] n, int t1, int c1, int t2, int c2, int c3) {
String[] nString = new String[4];
switch (t1) {
case 0:
nString[0] = calString2("" + (int) n[0], "" + (int) n[1], c1);
nString[1] = "" + (int) n[2];
nString[2] = "" + (int) n[3];
break;
case 1:
nString[0] = calString2("" + (int) n[0], "" + (int) n[2], c1);
nString[1] = "" + (int) n[1];
nString[2] = "" + (int) n[3];
break;
case 2:
nString[0] = calString2("" + (int) n[0], "" + (int) n[3], c1);
nString[1] = "" + (int) n[1];
nString[2] = "" + (int) n[2];
break;
case 3:
nString[0] = calString2("" + (int) n[1], "" + (int) n[2], c1);
nString[1] = "" + (int) n[0];
nString[2] = "" + (int) n[3];
break;
case 4:
nString[0] = calString2("" + (int) n[1], "" + (int) n[3], c1);
nString[1] = "" + (int) n[0];
nString[2] = "" + (int) n[2];
break;
default:
nString[0] = calString2("" + (int) n[2], "" + (int) n[3], c1);
nString[1] = "" + (int) n[0];
nString[2] = "" + (int) n[1];
}
if ((c2 / 3 c1 / 3 (t2 != 2 || c2 / 3 == c3 / 3))
|| ((c3 / 3 c1 / 3 + c2 / 3) t2 == 2)
|| (c3 == 1 c1 / 3 == 0)) // 特定情況下加上一個括號*****************************
nString[0] = '(' + nString[0] + ')';
switch (t2) {
case 0:
nString[0] = calString2(nString[0], "" + nString[1], c2);
nString[1] = nString[2];
break;
case 1:
nString[0] = calString2(nString[0], nString[2], c2);
break;
default:
nString[3] = nString[0];
nString[0] = calString2(nString[1], nString[2], c2);
nString[1] = nString[3];
}
if (c3 / 3 c2 / 3 || (c3 == 2 nString[0].indexOf('+') = 0)) // 特定情況下加上一個括號*****************************
nString[0] = '(' + nString[0] + ')';
return calString2(nString[0], nString[1], c3);
}
/** calString(),根據(jù)符號輸出一部運算表達式 */
private String calString2(String n1, String n2, int c) {
switch (c) {
case 0:
return n1 + '+' + n2;
case 1:
return n1 + '-' + n2;
case 2:
return n2 + '-' + n1;
case 3:
return n1 + '*' + n2;
case 4:
return n1 + '/' + n2;
default:
return n2 + '/' + n1;
}
}
}
import?java.util.ArrayList;?
import?java.util.Arrays;?
import?java.util.Collection;?
import?java.util.HashSet;?
import?java.util.List;?
import?java.util.Set;?
/**?
*?用給定的4個整數(shù)通過加減乘除運算得到24點,如果有多種情況,則全部輸出,如果不能得到24點,輸出提示br?
*?
*?@思路:將指定的4個數(shù)字進行全排列,將運算符‘+’、‘-’、‘*’、‘/’取3個進行所有情況排列,?
*?然后將所有的數(shù)字排列與所有的運算符排列按順序計算,?
*?如果最后計算結(jié)果等于想要的結(jié)果值比如24,則為符合條件的運算,?
*?將所有符合條件的數(shù)字排列和運算符排列存儲起來,并在最后打印輸出所有可能的情況?
*?
*?@author?chenjie?
*?
*/?
public?class?TwentyFourPoint?{?
public?static?void?main(String[]?args)?{?
try?{?
SetString?set?=?caculate(new?int[]?{?18,?18,?6,?12?},?24);?
printlnResultSet(set);?
}?catch?(Exception?e)?{?
//?e.printStackTrace();開發(fā)期間方便查找錯誤,測試通過后就無需打印錯誤信息了?
System.err.println(e.getMessage());?
}?
}?
/**?
*?打印結(jié)果集?
*?
*?@param?resultSet?
*?結(jié)果集?
*/?
private?static?void?printlnResultSet(CollectionString?resultSet)?{?
for?(String?str?:?resultSet)?{?
System.out.println(str);?
}?
}?
/**?
*?得到給定整形數(shù)組的全排列情況?
*?
*?@param?numbers?
*?給定的整形數(shù)組?
*?@return?全排列數(shù)組?
*/?
private?static?int[][]?arrangeAllNumbers(int[]?numbers)?{?
Listint[]?list?=?new?ArrayListint[]();?
allSort(numbers,?0,?numbers.length?-?1,?list);?
int[][]?resultSet?=?new?int[list.size()][list.get(0).length];?
resultSet?=?list.toArray(resultSet);?
return?resultSet;?
}?
/**?
*?得到給定的操作中出現(xiàn)的所有操作符排列情況?
*?
*?@param?operators?
*?出現(xiàn)的操作符數(shù)組?
*?@param?number?
*?每組操作符的數(shù)量?
*?@return?所有操作符排列數(shù)組?
*/?
private?static?char[][]?arrangeAllOperators(char[]?operators,?int?number)?{?
int?setSize?=?(int)?Math.pow(operators.length,?number);?
int?index?=?0;?
char[][]?resultSet?=?new?char[setSize][number];?
for?(int?i?=?0;?i??operators.length;?i++)?{?
for?(int?j?=?0;?j??operators.length;?j++)?{?
for?(int?k?=?0;?k??operators.length;?k++)?{?
resultSet[index][0]?=?operators[i];?
resultSet[index][1]?=?operators[j];?
resultSet[index][2]?=?operators[k];?
index++;?
}?
}?
}?
return?resultSet;?
}?
/**?
*?根據(jù)給定的一組整數(shù),通過加減乘除運算,得到想要的結(jié)果,如果可以得到結(jié)果,則返回所有可能的結(jié)果的運算形式。?
*?返回的運算形式,均按從左到右的順序計算,并不是遵循四則運算法則,比如:br?
*?輸出的結(jié)果形式為:br?
*?1?*?8?-?6?*?12?=?24?br?
*?表示的運算順序是:br?
*?1:1?*?8?=?8,br?
*?2:8?-?6?=?2,br?
*?3:2?*?12?=?24br?
*?而不是按照四則運算法則計算:br?
*?1:1?*?8?=?8,br?
*?2:6?*?12?=?72,br?
*?3:8?*?72?=?576br?
*?
*?
*?@param?numbers?
*?給定進行運算的一組整數(shù),4個數(shù)為一組?
*?@param?targetNumber?
*?想要得到的結(jié)果?
*?@return?所有可能得到想要的結(jié)果的所有運算形式的字符串形式集合?
*?@throws?Exception?
*?如果不能得到想要的結(jié)果,則拋出該異常,表明根據(jù)指定的一組數(shù)字通過一系列的加減乘除不能得到想要的結(jié)果?
*/?
public?static?SetString?caculate(int[]?numbers,?int?targetNumber)?
throws?Exception?{?
SetString?resultSet?=?new?HashSetString();//?這里用Set而不是用List,主要是因為當給定的一組數(shù)字中如果有重復(fù)數(shù)字的話,同一結(jié)果會被出現(xiàn)多次,如果用List存放的話,會將重復(fù)的結(jié)果都存放起來,而Set會自動消除重復(fù)值?
char[][]?operatorsArrangement?=?arrangeAllOperators(new?char[]?{?'+',?
'-',?'*',?'/'?},?3);?
int[][]?numbersArrangement?=?arrangeAllNumbers(numbers);?
for?(int[]?nums?:?numbersArrangement)?
for?(char[]?operators?:?operatorsArrangement)?{?
int?result?=?0;?
try?{?
result?=?caculate(nums,?operators);?
}?catch?(Exception?e)?{//?出現(xiàn)非精確計算?
continue;?
}?
if?(result?==?targetNumber)?
resultSet.add(buildString(nums,?operators,?targetNumber));//?如果計算后的結(jié)果等于想要的結(jié)果,就存放到集合中?
}?
if?(resultSet.isEmpty())?
throw?new?Exception("給定的數(shù)字:"?+?Arrays.toString(numbers)?
+?"不能通過加減乘除運算得到結(jié)果:"?+?targetNumber);?
return?resultSet;?
}?
/**?
*?將一組整型數(shù)字以給定的操作符按順序拼接為一個完整的表達式字符串?
*?
*?@param?nums?
*?一組整型數(shù)字?
*?@param?operators?
*?一組操作符?
*?@param?target?
*?目標值?
*?@return?拼接好的表達式字符串?
*/?
private?static?String?buildString(int[]?nums,?char[]?operators,?int?target)?{?
String?str?=?String.valueOf(nums[0]);?
for?(int?i?=?0;?i??operators.length;?i++)?{?
str?=?str?+?'?'?+?operators[i]?+?'?'?+?nums[i?+?1];?
}?
str?=?str?+?"?=?"?+?target;?
return?str;?
}?
/**?
*?將給定的一組數(shù)字以給定的操作符按順序進行運算,如:int?result?=?caculate(new?int[]{3,4,5,8},?new?
*?char[]{'+','-','*'});?
*?
*?@param?nums?
*?一組數(shù)字?
*?@param?operators?
*?一組運算符,數(shù)量為數(shù)字的個數(shù)減1?
*?@return?最后的計算結(jié)果?
*?@throws?Exception?
*?當計算結(jié)果不精確時,拋出該異常,主要是針對除法運算,例如18?/?8?=?2,諸如這樣不精確計算將拋出該異常?
*/?
private?static?int?caculate(int[]?nums,?char[]?operators)?throws?Exception?{?
int?result?=?0;?
for?(int?i?=?0;?i??operators.length;?i++)?{?
if?(i?==?0)?{?
result?=?caculate(nums[i],?nums[i?+?1],?operators[i]);?
}?else?{?
result?=?caculate(result,?nums[i?+?1],?operators[i]);?
}?
}?
return?result;?
}?
/**?
*?根據(jù)指定操作符將兩個給定的數(shù)字進行計算?
*?
*?@param?num1?
*?數(shù)字1?
*?@param?num2?
*?數(shù)字2?
*?@param?operator?
*?操作符,只能從“+、-、*、/”4個操作符中取值?
*?@return?計算結(jié)果?
*?@throws?Exception?
*?當計算結(jié)果不精確時,拋出該異常,主要是針對除法運算,例如18?/?8?=?2,諸如這樣不精確計算將拋出該異常?
*/?
private?static?int?caculate(int?num1,?int?num2,?char?operator)?
throws?Exception?{?
double?result?=?0;?
switch?(operator)?{//?根據(jù)操作符做相應(yīng)的計算操作?
case?'+':?
result?=?num1?+?num2;?
break;?
case?'-':?
result?=?num1?-?num2;?
break;?
case?'*':?
result?=?num1?*?num2;?
break;?
case?'/':?
result?=?(double)?num1?/?(double)?num2;?
break;?
}?
if?(!check(result))?
throw?new?Exception("不精確的計算數(shù)字");?
return?(int)?result;?
}?
/**?
*?檢查指定的浮點數(shù)是否可以直接轉(zhuǎn)換為整型數(shù)字而不損失精度?
*?
*?@param?result?
*?要檢查的浮點數(shù)?
*?@return?如果可以進行無損轉(zhuǎn)換,返回true,否則返回false?
*/?
private?static?boolean?check(double?result)?{?
String?str?=?String.valueOf(result);?
int?pointIndex?=?str.indexOf(".");//?小數(shù)點的下標值?
String?fraction?=?str.substring(pointIndex?+?1);?
return?fraction.equals("0")???true?:?false;//?通過判斷小數(shù)點后是否只有一個0來確定是否可以無損轉(zhuǎn)換為整型數(shù)值?
}?
/**?
*?對傳入的整型數(shù)組buf進行全排列?
*?
*?@param?buf?
*?要進行全排列的整型數(shù)組?
*?@param?start?
*?開始的下標值?
*?@param?end?
*?結(jié)束下標值?
*?@param?list?
*?保存最后全排列結(jié)果的集合?
*/?
private?static?void?allSort(int[]?buf,?int?start,?int?end,?Listint[]?list)?{?
if?(start?==?end)?{//?當只要求對數(shù)組中一個字母進行全排列時,只要就按該數(shù)組輸出即可?
int[]?a?=?new?int[buf.length];?
System.arraycopy(buf,?0,?a,?0,?a.length);?
list.add(a);?
}?else?{//?多個字母全排列?
for?(int?i?=?start;?i?=?end;?i++)?{?
int?temp?=?buf;//?交換數(shù)組第一個元素與后續(xù)的元素?
buf?=?buf[i];?
buf[i]?=?temp;?
allSort(buf,?start?+?1,?end,?list);//?后續(xù)元素遞歸全排列?
temp?=?buf;//?將交換后的數(shù)組還原?
buf?=?buf[i];?
buf[i]?=?temp;?
}?
}?
}?
}?
C的代碼要嗎?我對java不是很熟,我試著用java寫下吧。給我點時間!
package test.cardgame;
public class BinaryTreeNode
{
private BinaryTreeNode leftSon=null;
private BinaryTreeNode rightSon=null;
private BinaryTreeNode parent=null;
private double data=0;
private int sign=-1;
public int getSign()
{
return sign;
}
public void setSign(int sign)
{
this.sign = sign;
}
public BinaryTreeNode(BinaryTreeNode parent,BinaryTreeNode leftSon,BinaryTreeNode rightSon)
{
this.parent=parent;
this.leftSon=leftSon;
this.rightSon=rightSon;
}
public BinaryTreeNode()
{
}
public BinaryTreeNode getLeftSon()
{
return leftSon;
}
public void setLeftSon(BinaryTreeNode leftSon)
{
this.leftSon = leftSon;
leftSon.setParent(this);
}
public BinaryTreeNode getParent()
{
return parent;
}
public void setParent(BinaryTreeNode parent)
{
this.parent = parent;
}
public BinaryTreeNode getRightSon()
{
return rightSon;
}
public void setRightSon(BinaryTreeNode rightSon)
{
this.rightSon = rightSon;
rightSon.setParent(this);
}
public boolean isLeaf()
{
return (this.leftSon==nullthis.rightSon==null);
}
public boolean isRoot()
{
return this.parent==null;
}
public double getData()
{
return data;
}
public void setData(double data)
{
this.data = data;
}
}
package test.cardgame;
import java.util.ArrayList;
public class CardGame
{
private ArrayListString expressions=new ArrayListString();
public void solute(ArrayListBinaryTreeNode nodes,double target)
{
//whether the root data equals target
if (nodes.size()==1)
{
if (nodes.get(0).getData()==target)
{
String expression=printBinaryTree(nodes.get(0));
addExpression(expression);
return;
}
}
for (int i=0;inodes.size();i++)
{
for (int j=0;jnodes.size();j++)
{
if (i==j)
{
continue;
}
for (int k=0;k4;k++)
{
BinaryTreeNode node=new BinaryTreeNode();
BinaryTreeNode leftSon=nodes.get(i);
BinaryTreeNode rightSon=nodes.get(j);
if (k==0)
{
node.setData(leftSon.getData()+rightSon.getData());
}
else if (k==1)
{
node.setData(leftSon.getData()-rightSon.getData());
}
else if (k==2)
{
node.setData(leftSon.getData()*rightSon.getData());
}
else if (k==3)
{
if (rightSon.getData()==0)
{
continue;
}
node.setData(leftSon.getData()/rightSon.getData());
}
node.setLeftSon(leftSon);
node.setRightSon(rightSon);
node.setSign(k);
ArrayListBinaryTreeNode clonedArrayList=cloneArrayList(nodes);
//remove nodes from the tree
clonedArrayList.remove(leftSon);
clonedArrayList.remove(rightSon);
clonedArrayList.add(node);
solute(clonedArrayList,target);
}
}
}
}
public void printResult()
{
for (int i=0;iexpressions.size();i++)
{
System.out.println("Solution "+i+": "+expressions.get(i));
}
}
private void addExpression(String expression)
{
if (expressions.contains(expression))
{
return;
}
expressions.add(expression);
}
private ArrayListBinaryTreeNode cloneArrayList(ArrayListBinaryTreeNode source)
{
ArrayListBinaryTreeNode result=new ArrayListBinaryTreeNode();
for (int i=0;isource.size();i++)
{
result.add(source.get(i));
}
return result;
}
private String printBinaryTree(BinaryTreeNode resultRoot)
{
if (resultRoot.isLeaf())
{
return doubleToString(resultRoot.getData());
}
else
{
String expression="(";
expression+=printBinaryTree(resultRoot.getLeftSon());
int sign=resultRoot.getSign();
if (sign==0)
{
expression+="+";
}
else if (sign==1)
{
expression+="-";
}
else if (sign==2)
{
expression+="*";
}
else if (sign==3)
{
expression+="/";
}
expression+=printBinaryTree(resultRoot.getRightSon());
expression+=")";
return expression;
}
}
private String doubleToString(double value)
{
int intValue=(int)value;
if (value==intValue)
{
return String.valueOf(intValue);
}
else
{
return String.valueOf(value);
}
}
public BinaryTreeNode buildBinaryTreeNode(double value)
{
BinaryTreeNode node=new BinaryTreeNode();
node.setData(value);
return node;
}
public static void main(String[] args)
{
CardGame cardGame=new CardGame();
ArrayListBinaryTreeNode nodes=new ArrayListBinaryTreeNode();
nodes.add(cardGame.buildBinaryTreeNode(4));
nodes.add(cardGame.buildBinaryTreeNode(6));
nodes.add(cardGame.buildBinaryTreeNode(1));
nodes.add(cardGame.buildBinaryTreeNode(1));
cardGame.solute(nodes, 24);
cardGame.printResult();
}
}
24點的源代碼,因該可以計算出4則運算24 public class Test24Point{ public static void main(String[] args){ int index = 0 ; int temp = 0 ; int totalSUC = 0 ; int numb[] = new int[4];//the first four numbers double num[][] = new double[36][3];//three numbers after calculating double total[] = new double[6];//the number after three steps of calculating double p[][] = new double[6][8]; double q[][] = new double[3][7]; //System.out.println(2465%108); //System.out.println(2465/108); System.out.println("\"a--b\"means\"b-a\""); System.out.println("\"a//b\"means\"b/a\"\n"); /* for(int h = 0; h = 9; h ++)//Get the first four numbers for calculating and store into the array numb[4]; for(int i = 0; i = 9; i ++) for(int j = 0; j = 9; j ++) for(int k = 0; k = 9; k ++){ numb[0] = h ; numb[1] = i ; numb[2] = j ; numb[3] = k ; }*/ for(int i = 0 ; i 4 ; i ++){ numb = Integer.parseInt(args); } for(int i = 0; i 3; i ++)//Get two of the four to calculate and then store the new number into the array p; for(int j = i + 1; j 4 ; j ++,temp ++){ p[temp][0] = numb + numb[j]; p[temp][1] = numb - numb[j]; p[temp][2] = numb[j] - numb; p[temp][3] = numb * numb[j]; if(numb[j] != 0) p[temp][4] = numb / (double)numb[j]; else p[temp][4] = 10000; if(numb != 0) p[temp][5] = numb[j] / (double)numb; else p[temp][5] = 10000;