# 24点游戏算法

24点游戏算法 问题描述：给出4个1-10的数字，通过加减乘除，得到数字为24就算胜利

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
int[] arr=new int[4];
for(int i=0;i<4;i++)
{
arr[i]=input.nextInt();
}
System.out.println(getResult(arr, 0, 0));
}
public static boolean getResult(int[] arr,int i,int result)
{

if(result==24)
return true;
if(i<arr.length)
{

return getResult(arr, i+1, result+arr[i])
||getResult(arr, i+1, result-arr[i])
||getResult(arr, i+1, result==0?1:result*arr[i])
||getResult(arr, i+1, result==0?1:result/arr[i]);

}else {
return false;
}
}
}  

import java.util.Scanner;

public class Main {
private static boolean result;
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int[] source = new int[4];

for(int i = 0;i < 4; i++){
source[i] = in.nextInt();
}
cal24(source,1,source[0]);
if(result){
System.out.println("true");
}else{
System.out.println("false");
}
in.close();
}
public static void cal24(int[] s,int start,int sum){
if(start == 4 && sum == 24){
result =true;
}else if(start<4){
cal24(s,start+1,sum+=s[start]);
cal24(s,start+1,sum-=s[start]);
cal24(s,start+1,sum*=s[start]);
if(sum!=0)
cal24(s,start+1,sum/=s[start]);
}

}
}  

import java.util.LinkedList;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int c=sc.nextInt();
int d=sc.nextInt();
cal(l,c);
l.clear();
//System.out.print(l);
cal(l,d);
if(l1.contains(24))
System.out.print("true");
else
System.out.print("false");

}

private static void cal(LinkedList<Integer> l, int c) {
// TODO Auto-generated method stub
l1.clear();
for(int i=0;i<l.size();i++)
{
}
}

}  

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String strNums = scanner.nextLine();
String[] nums = strNums.split(" ");
int a = Integer.parseInt(nums[0]);
int b = Integer.parseInt(nums[2]);
int c = Integer.parseInt(nums[2]);
int d = Integer.parseInt(nums[3]);
if (a >= 1 && a <= 10 && b >= 1 && b <= 10 && c >= 1 && c <= 10 && d >= 1 && d <= 10) {
game24Points(a, b, c, d);
}
scanner.close();
}

/**
* 24点游戏算法 问题描述：给出4个1-10的数字，通过加减乘除，得到数字为24就算胜利
* 输入：4个1-10的数字。[数字允许重复，测试用例保证无异常数字] 输出： true or false
*/
public static void game24Points(int a, int b, int c, int d) {
int[] nums = { a, b, c, d };
if (Search(nums.length, nums)) {
System.out.println("true");
} else {
System.out.println("false");
}
}

public static boolean Search(int n, int[] number) {
if (n == 1) {
if (Math.abs((number[0] - 24)) == 0)
return true;
else
return false;
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) // 共 C(n,2) 种排列
{
int a, b;
// 现场临时保存
a = number[i];
b = number[j];
number[j] = number[n - 1];
number[i] = a + b;
if (Search(n - 1, number))
return true;
number[i] = a - b;
if (Search(n - 1, number))
return true;
number[i] = b - a;
if (Search(n - 1, number))
return true;
number[i] = a * b;
if (Search(n - 1, number))
return true;
if (b != 0) // 除数不为0
{
number[i] = a / b;
if (Search(n - 1, number))
return true;
}
if (a != 0) {
number[i] = b / a;
if (Search(n - 1, number))
return true;
}
// 现场恢复
number[i] = a;
number[j] = b;
}
}
return false;
}
}
• 本文已收录于以下专栏：

举报原因： 您举报文章：24点游戏算法 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)