关闭

24点游戏算法

444人阅读 评论(0) 收藏 举报
分类:

描述:
24点游戏算法 问题描述:给出4个1-10的数字,通过加减乘除,得到数字为24就算胜利
输入:4个1-10的数字。[数字允许重复,测试用例保证无异常数字]
输出: true or false

代码:
一,

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 {  
    static LinkedList<Integer> l1=new LinkedList<Integer>();  
    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();  
        LinkedList<Integer> l=new LinkedList<Integer>();  
        l.add(a*b);  
        l.add(a-b);  
        l.add(a+b);  
        l.add(a/b);  
        cal(l,c);  
        l.clear();  
        l=(LinkedList<Integer>) l1.clone();  
        //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++)  
        {  
            l1.add(l.get(i)+c);  
            l1.add(l.get(i)-c);  
            l1.add(l.get(i)*c);  
            l1.add(l.get(i)/c);  
        }  
    }  

}  

四,

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;
    }
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:34998次
    • 积分:1065
    • 等级:
    • 排名:千里之外
    • 原创:71篇
    • 转载:13篇
    • 译文:2篇
    • 评论:1条
    最新评论