真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

包含24點(diǎn)速算java代碼的詞條

算24點(diǎn) java代碼

C的代碼要嗎?我對(duì)java不是很熟,我試著用java寫下吧。給我點(diǎn)時(shí)間!

成都創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比云霄網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式云霄網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋云霄地區(qū)。費(fèi)用合理售后完善,10余年實(shí)體公司更值得信賴。

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

}

}

java程序設(shè)計(jì):算24點(diǎn)

//這是我自己寫的,在VC里可以運(yùn)行。

#include "stdafx.h"

#includestdio.h

#includestdlib.h

static int NUMBER;

bool Game24(int const nNum, int* arr, int nLen, int nCount, char* pOperator, bool* pFlag){

if(nCount == 1){

if(*arr == nNum){

printf("((%d %c %d) %c %d) %c %d = %d\n",

arr[0],pOperator[0],arr[1],pOperator[1],arr[2],pOperator[2],arr[3],NUMBER);

if(!(*pFlag)) *pFlag = true;

}

return *pFlag;

}

for(int i = 0; i 4; ++i){

switch(i){

case 0:

pOperator[nCount - 2] = '+';

Game24(nNum - arr[nCount - 1], arr,nLen,nCount - 1,pOperator,pFlag);

break;

case 1:

pOperator[nCount - 2] = '-';

Game24(nNum + arr[nCount - 1], arr,nLen,nCount - 1,pOperator,pFlag);

break;

case 2:

pOperator[nCount - 2] = '*';

if( arr[nCount - 1] !(nNum % arr[nCount - 1]))

Game24(nNum / arr[nCount - 1], arr,nLen,nCount - 1,pOperator,pFlag);

break;

case 3:

pOperator[nCount - 2] = '/';

if(arr[nCount - 1])

Game24(nNum * arr[nCount - 1], arr,nLen,nCount - 1,pOperator,pFlag);

break;

}

}

return *pFlag;

}

int fOperating(char pOpe, int x1, int x2){

switch(pOpe){

case '+': return x1 + x2;

case '-': return x1 - x2;

case '*': return x1 * x2;

case '/': return x1 / x2;

}

}

bool fGame24(int const nNum, int* arr,char* pOperator, bool* pFlag){

int nLeft = 0,nRight = 0;

char pOpe[4] = {'+','-','*','/'};

for(int l = 0; l 4; ++l){

if(l == 3 (!arr[1] || arr[0] % arr[1])) continue;

pOperator[0] = pOpe[l];

nLeft = fOperating(pOpe[l],arr[0],arr[1]);

for(int r = 0; r 4; ++r){

if(r == 3 (!arr[3] || arr[2] % arr[3])) continue;

pOperator[2] = pOpe[r];

nRight = fOperating(pOpe[r],arr[2],arr[3]);

for(int m = 0; m 4; ++m){

if(m == 3 (!nRight || nLeft % nRight)) continue;

pOperator[1] = pOpe[m];

if(fOperating(pOpe[m],nLeft,nRight) == nNum){

printf("(%d %c %d) %c (%d %c %d)? = %d\n",

arr[0],pOperator[0],arr[1],pOperator[1],arr[2],pOperator[2],arr[3],NUMBER);

if(!(*pFlag)) *pFlag = true;

}

}

}

}

return *pFlag;

}

int main(int argc, char* argv[])

{

puts("start!\nPlease input 4 numbers:");

bool* pFlag = (bool*)malloc(1);

*pFlag = false;

bool flag = 0;

int pNum[4] = {0};

int cNum[4] = {0};

int iNum[4] = {0};

char cOpe[3] = {0};

for(int i = 0; i 4; ++i)

scanf("%d",pNum[i]);

puts("So, what number do you want:");

scanf("%d",NUMBER);

puts("************************************");

for(iNum[0] = 0; iNum[0] 4; ++iNum[0]){

for(iNum[1] = 0; iNum[1] 4; ++iNum[1]){

if(iNum[1] == iNum[0]) continue;

for(iNum[2] = 0; iNum[2] 4; ++iNum[2]){

if(iNum[2] == iNum[0] || iNum[2] == iNum[1]) continue;

for(iNum[3] = 0; iNum[3] 4; ++iNum[3]){

if(iNum[3] == iNum[0] || iNum[3] == iNum[1] || iNum[3] == iNum[2]) continue;

for(int i = 0; i 4; ++i) cNum[i] = pNum[iNum[i]];

if(Game24(NUMBER,cNum,4,4,cOpe,pFlag)) flag = true;

if(fGame24(NUMBER,cNum,cOpe,pFlag)) flag = true;

}

}

}

}

free(pFlag);

if(!flag) printf("No way can be found.\n");

puts("************************************\nEnd!");

system("pause");

return 0;

}

java算24點(diǎn)代碼:輸入4個(gè)數(shù)算24點(diǎn),能夠在命令提示符下就可以運(yùn)行。100多

import java.util.Scanner;

/** 給定4個(gè)數(shù)字計(jì)算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;

/**

* 該對(duì)象擁有3個(gè)私有變量 expressionResult,所需結(jié)果 maxLine,輸出結(jié)果每頁(yè)行數(shù) error,是否出錯(cuò)

* numbers[4],記錄用來運(yùn)算的4個(gè)數(shù)

*

* 其次,該對(duì)象擁有以下方法供外部調(diào)用 setNumbers(double[] 運(yùn)算的數(shù)) 輸入用來運(yùn)算的數(shù),4個(gè)時(shí)才能計(jì)算,無(wú)返回

* setMaxLine(int 行數(shù)) 輸入每頁(yè)的行數(shù),無(wú)返回 getMaxLine() 返回每頁(yè)的行數(shù),類型為int

* setExpressionResult(double 所需結(jié)果) 輸入所需結(jié)果,無(wú)返回 getExpressionResult()

* 返回所需結(jié)果,類型為double getExpression() 返回可得出所需結(jié)果的表達(dá)式,類型為字符串?dāng)?shù)組

*

* 最后,私有方法均為計(jì)算與表達(dá)式轉(zhuǎn)換部分

*/

// 測(cè)試使用

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

int[] arr = new int[4];

System.out.print("輸入第一個(gè)數(shù):");

arr[0] = scanner.nextInt();

System.out.print("輸入第二個(gè)數(shù):");

arr[1] = scanner.nextInt();

System.out.print("輸入第三個(gè)數(shù):");

arr[2] = scanner.nextInt();

System.out.print("輸入第四個(gè)數(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è)定被計(jì)算的四個(gè)數(shù),由于是數(shù)組,所以具有容錯(cuò)功能(不為4個(gè)數(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è)定每頁(yè)顯示的行數(shù) */

// public void setMaxLine(int n) {

// if (n0) {

// maxLine=n;

// }

// }

// /** 返回每頁(yè)顯示的行數(shù) */

// public int getMaxLine() {

// return maxLine;

// }

/** 設(shè)定需要得到的結(jié)果 */

public void setExpressionResult(double n) {

expressionResult = n;

}

/** 返回所需結(jié)果 */

public double expressionResult() {

return expressionResult;

}

/** 返回符合條件的表達(dá)式 */

public String[] getExpression() {

if (!error) {

String[] expression = calculate(numbers);

return expression;

} else

return new String[] { "出錯(cuò)了,輸入有誤" };

}

/** cal24(),輸出結(jié)果為24的表達(dá)式 */

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組解,暫時(shí)未溢出

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

// 去除連減連除的解,因?yàn)閤/(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ù)無(wú)解" };

String[] resultReturn = new String[count];

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

return resultReturn;

}

/** cal1(),將4個(gè)數(shù)計(jì)算一次后返回3個(gè)數(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個(gè)數(shù)計(jì)算一次后返回2個(gè)數(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個(gè)數(shù)計(jì)算后返回結(jié)果 */

private double cal(double n1, double n2, int c) { // n1,n2為運(yùn)算數(shù),c為運(yùn)算類型

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; // 使計(jì)算結(jié)果必不為24

else

return n1 / n2;

default:

if (n1 == 0)

return 9999; // 同上

else

return n2 / n1;

}

}

/** calString(),輸出表達(dá)式 */

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)) // 特定情況下加上一個(gè)括號(hào)*****************************

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)) // 特定情況下加上一個(gè)括號(hào)*****************************

nString[0] = '(' + nString[0] + ')';

return calString2(nString[0], nString[1], c3);

}

/** calString(),根據(jù)符號(hào)輸出一部運(yùn)算表達(dá)式 */

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點(diǎn)的代碼!進(jìn)來看!有特別要求!

package ceshi;

/** 給定4個(gè)數(shù)字計(jì)算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;

/**

* 該對(duì)象擁有3個(gè)私有變量 expressionResult,所需結(jié)果 maxLine,輸出結(jié)果每頁(yè)行數(shù) error,是否出錯(cuò)

* numbers[4],記錄用來運(yùn)算的4個(gè)數(shù)

*

* 其次,該對(duì)象擁有以下方法供外部調(diào)用 setNumbers(double[] 運(yùn)算的數(shù)) 輸入用來運(yùn)算的數(shù),4個(gè)時(shí)才能計(jì)算,無(wú)返回

* setMaxLine(int 行數(shù)) 輸入每頁(yè)的行數(shù),無(wú)返回 getMaxLine() 返回每頁(yè)的行數(shù),類型為int

* setExpressionResult(double 所需結(jié)果) 輸入所需結(jié)果,無(wú)返回 getExpressionResult()

* 返回所需結(jié)果,類型為double getExpression() 返回可得出所需結(jié)果的表達(dá)式,類型為字符串?dāng)?shù)組

*

* 最后,私有方法均為計(jì)算與表達(dá)式轉(zhuǎn)換部分

*/

// 測(cè)試使用

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

}

}

/** 設(shè)定被計(jì)算的四個(gè)數(shù),由于是數(shù)組,所以具有容錯(cuò)功能(不為4個(gè)數(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è)定每頁(yè)顯示的行數(shù) */

// public void setMaxLine(int n) {

// if (n0) {

// maxLine=n;

// }

// }

// /** 返回每頁(yè)顯示的行數(shù) */

// public int getMaxLine() {

// return maxLine;

// }

/** 設(shè)定需要得到的結(jié)果 */

public void setExpressionResult(double n) {

expressionResult = n;

}

/** 返回所需結(jié)果 */

public double expressionResult() {

return expressionResult;

}

/** 返回符合條件的表達(dá)式 */

public String[] getExpression() {

if (!error) {

String[] expression = calculate(numbers);

return expression;

} else

return new String[] { "出錯(cuò)了,輸入有誤" };

}

/** cal24(),輸出結(jié)果為24的表達(dá)式 */

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組解,暫時(shí)未溢出

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

// 去除連減連除的解,因?yàn)閤/(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ù)無(wú)解" };

String[] resultReturn = new String[count];

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

return resultReturn;

}

/** cal1(),將4個(gè)數(shù)計(jì)算一次后返回3個(gè)數(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個(gè)數(shù)計(jì)算一次后返回2個(gè)數(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個(gè)數(shù)計(jì)算后返回結(jié)果 */

private double cal(double n1, double n2, int c) { // n1,n2為運(yùn)算數(shù),c為運(yùn)算類型

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; // 使計(jì)算結(jié)果必不為24

else

return n1 / n2;

default:

if (n1 == 0)

return 9999; // 同上

else

return n2 / n1;

}

}

/** calString(),輸出表達(dá)式 */

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)) // 特定情況下加上一個(gè)括號(hào)*****************************

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)) // 特定情況下加上一個(gè)括號(hào)*****************************

nString[0] = '(' + nString[0] + ')';

return calString2(nString[0], nString[1], c3);

}

/** calString(),根據(jù)符號(hào)輸出一部運(yùn)算表達(dá)式 */

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;

}

}

}

一個(gè)java面試題:計(jì)算24點(diǎn)游戲 編程

import java.util.Random;

public class test2

{

public static void main(String[] args)

{

Random random = new Random();

int a[] = new int[4];

for(int i=0; i4; i++)

{

a[i] = random.nextInt(13)+1;

}

for(int j=0; j4; j++)

{

System.out.println("第" + j +"個(gè)數(shù):" + a[j]);

}

Calcula(a);

}

public static void Calcula(int[] a)

{

int add, sub, multi, div;

add = 0;

sub = 0;

multi = 0;

div = 0;

for(int i=0; i4; i++)

{

add = add + a[i];

sub = sub - a[i];

multi = multi * a[i];

div = div/a[i];

}

if(add==24)

{

System.out.println(a[0] + "+" + a[1] + "+" + a[2] + "+" + a[3]

+ "=" + add);

}

else if(sub==24)

{

System.out.println(a[0] + "-" + a[1] + "-" + a[2] + "-" + a[3]

+ "=" + sub);

}

else if(multi==24)

{

System.out.println(a[0] + "*" + a[1] + "*" + a[2] + "*" + a[3]

+ "=" + multi);

}

else if(div==24)

{

System.out.println(a[0] + "÷" + a[1] + "÷" + a[2] + "÷" + a[3]

+ "=" + div);

}

else

{

System.out.println("對(duì)不起,沒有實(shí)現(xiàn)24點(diǎn)的數(shù)");

}

}

}

已編譯通過~

用JAVA如何算出24點(diǎn)?

24點(diǎn)的源代碼,因該可以計(jì)算出4則運(yùn)算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;


網(wǎng)站欄目:包含24點(diǎn)速算java代碼的詞條
文章網(wǎng)址:http://weahome.cn/article/hpsphh.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部