java24点代码的简单介绍-古蔺大橙子建站
RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
java24点代码的简单介绍

用JAVA如何算出24点?

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;

在庄浪等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都网站设计、做网站 网站设计制作按需定制,公司网站建设,企业网站建设,品牌网站制作,网络营销推广,成都外贸网站制作,庄浪网站建设费用合理。

急求 JAVA的 算24点的代码!进来看!有特别要求!

package ceshi;

/** 给定4个数字计算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,所需结果 maxLine,输出结果每页行数 error,是否出错

* numbers[4],记录用来运算的4个数

*

* 其次,该对象拥有以下方法供外部调用 setNumbers(double[] 运算的数) 输入用来运算的数,4个时才能计算,无返回

* setMaxLine(int 行数) 输入每页的行数,无返回 getMaxLine() 返回每页的行数,类型为int

* setExpressionResult(double 所需结果) 输入所需结果,无返回 getExpressionResult()

* 返回所需结果,类型为double getExpression() 返回可得出所需结果的表达式,类型为字符串数组

*

* 最后,私有方法均为计算与表达式转换部分

*/

// 测试使用

public static void main(String[] args) {

Core s = new Core();

s.setNumbers(new int[] { 3, 4, 8, 6 });

String[] output = s.getExpression();

for (int i = 0; i output.length; i++) {

System.out.println(output[i]);

}

}

/** 设定被计算的四个数,由于是数组,所以具有容错功能(不为4个数) */

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;

}

/** 设定每页显示的行数 */

// public void setMaxLine(int n) {

// if (n0) {

// maxLine=n;

// }

// }

// /** 返回每页显示的行数 */

// public int getMaxLine() {

// return maxLine;

// }

/** 设定需要得到的结果 */

public void setExpressionResult(double n) {

expressionResult = n;

}

/** 返回所需结果 */

public double expressionResult() {

return expressionResult;

}

/** 返回符合条件的表达式 */

public String[] getExpression() {

if (!error) {

String[] expression = calculate(numbers);

return expression;

} else

return new String[] { "出错了,输入有误" };

}

/** cal24(),输出结果为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])) { // 去除完全重复的解

isRepeat = true;

break; // 提前退出循环

}

}

if (c1 == c2 c2 == c3 c1 % 3 == 0

t1 + t2 != 0) { // 连加连乘

isRepeat = true;

}

if (!isRepeat) {

count++;

}

}

}

}

}

}

}

if (count == 0)

return new String[] { "该组数无解" };

String[] resultReturn = new String[count];

System.arraycopy(resultString, 0, resultReturn, 0, count);

return resultReturn;

}

/** cal1(),将4个数计算一次后返回3个数 */

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个数计算一次后返回2个数 */

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个数计算后返回结果 */

private double cal(double n1, double n2, int c) { // n1,n2为运算数,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; // 使计算结果必不为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(),根据符号输出一部运算表达式 */

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;

}

}

}

java算24点代码:输入4个数算24点,能够在命令提示符下就可以运行。100多

import java.util.Scanner;

/** 给定4个数字计算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,所需结果 maxLine,输出结果每页行数 error,是否出错

* numbers[4],记录用来运算的4个数

*

* 其次,该对象拥有以下方法供外部调用 setNumbers(double[] 运算的数) 输入用来运算的数,4个时才能计算,无返回

* setMaxLine(int 行数) 输入每页的行数,无返回 getMaxLine() 返回每页的行数,类型为int

* setExpressionResult(double 所需结果) 输入所需结果,无返回 getExpressionResult()

* 返回所需结果,类型为double getExpression() 返回可得出所需结果的表达式,类型为字符串数组

*

* 最后,私有方法均为计算与表达式转换部分

*/

// 测试使用

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

int[] arr = new int[4];

System.out.print("输入第一个数:");

arr[0] = scanner.nextInt();

System.out.print("输入第二个数:");

arr[1] = scanner.nextInt();

System.out.print("输入第三个数:");

arr[2] = scanner.nextInt();

System.out.print("输入第四个数:");

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

}

}

/** 设定被计算的四个数,由于是数组,所以具有容错功能(不为4个数) */

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;

}

/** 设定每页显示的行数 */

// public void setMaxLine(int n) {

// if (n0) {

// maxLine=n;

// }

// }

// /** 返回每页显示的行数 */

// public int getMaxLine() {

// return maxLine;

// }

/** 设定需要得到的结果 */

public void setExpressionResult(double n) {

expressionResult = n;

}

/** 返回所需结果 */

public double expressionResult() {

return expressionResult;

}

/** 返回符合条件的表达式 */

public String[] getExpression() {

if (!error) {

String[] expression = calculate(numbers);

return expression;

} else

return new String[] { "出错了,输入有误" };

}

/** cal24(),输出结果为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])) { // 去除完全重复的解

isRepeat = true;

break; // 提前退出循环

}

}

if (c1 == c2 c2 == c3 c1 % 3 == 0

t1 + t2 != 0) { // 连加连乘

isRepeat = true;

}

if (!isRepeat) {

count++;

}

}

}

}

}

}

}

if (count == 0)

return new String[] { "该组数无解" };

String[] resultReturn = new String[count];

System.arraycopy(resultString, 0, resultReturn, 0, count);

return resultReturn;

}

/** cal1(),将4个数计算一次后返回3个数 */

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个数计算一次后返回2个数 */

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个数计算后返回结果 */

private double cal(double n1, double n2, int c) { // n1,n2为运算数,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; // 使计算结果必不为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(),根据符号输出一部运算表达式 */

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;

}

}

}

算24点 java代码

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

}

}


名称栏目:java24点代码的简单介绍
标题来源:http://scgulin.cn/article/ddedghs.html