用JAVA如何算出24点?

用JAVA如何算出24点?,第1张

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 = 0h <= 9h ++)//Get the first four numbers for calculating and store into the array numb[4]for(int i = 0i <= 9i ++) for(int j = 0j <= 9j ++) for(int k = 0k <= 9k ++){ 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 = 0i <3i ++)//Get two of the four to calculate and then store the new number into the array p for(int j = i + 1j <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

//这是我自己写的,在VC里陆漏可以运早皮烂行。

#include "stdafx.h"

#include<stdio.h>

#include<stdlib.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 = 0i <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 = 0l <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 = 0r <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 = 0m <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 = 0i <4++i)

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

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

scanf("%d",&NUMBER)

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

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

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

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

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

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

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

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

for(int i = 0i <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

}

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=0i<4i++)

{

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

}

for(int j=0j<4j++)

{

System.out.println("第" + j +"个数:" + 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=0i<4i++)

{

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("对不起,液弯没有实现24点的数")

}

}

}

已编译通过~


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12439671.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存