蓝桥杯——ALGO995——24点

通过万岁!!!

  • 题目:就是给你四张扑克牌,然后尽可能的通过加减乘除和括号得到小于等于24的最大值,最大就是24。注意,除法的时候需要判断是不是整除。
  • 思路:猛一看,感觉可能有点难,特别是加括号,这可如何是好。但是再怎么加括号,也是两个数一块运算呗。所以说,每次找两个数,然后进行计算得到一个ans,然后再找一个进行计算…,直到4个数都用完。并且我们记录每次得到的最大的满足条件的数。上面的思路其实就是a,b计算得到ans,然后ans与c计算得到ans,ans再与d计算,得到ans,最终比较max和ans。但是存在一个问题,例如(a+b)*(c+d)的时候,这时候,需要并行的先计算两部分,而之前的思路类似串行计算。这个也简单,我们当计算完a,b的时候,再把这种情况考虑进去就行了,拿到剩下两个c,d计算,然后两个计算结果再计算。
  • 技巧:这里其实用到了深度优先,但是这个题的并行(a+b)*(c+d),是需要额外考虑的。
  • 题目链接:http://lx.lanqiao.cn/problem.page?gpid=T2979

java代码

import java.util.Scanner;


/**
* @创建人 xcs
* @创建日期 2022/4/3
* @创建时间 15:01
* 题目链接:http://lx.lanqiao.cn/problem.page?gpid=T2979
*/
public class ALGO995 {
    static int max = 0;


    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = Integer.parseInt(scanner.nextLine());
        for (int i = 0; i < n; i++) {
            max = 0;
            int[] nums = new int[4];
            nums[0] = Integer.parseInt(scanner.nextLine());
            nums[1] = Integer.parseInt(scanner.nextLine());
            nums[2] = Integer.parseInt(scanner.nextLine());
            nums[3] = Integer.parseInt(scanner.nextLine());
            // 初始化全false
            boolean[] vis = new boolean[4];
            for (int j = 0; j < 4; j++) {
                vis[j] = true;
                getAns(vis, nums, 1, nums[j]);
                vis[j] = false;
            }
            System.out.println(max);
        }
    }

    private static void getAns(boolean[] vis, int[] nums, int flag, int ans) {
        if (max == 24) {
            return;
        }
        if (4 == flag) {
            max = ans > max && ans <= 24 ? ans : max;
        }
        if (2 == flag) {
            // 选了两个了,然后选择剩下两个(a+b)*(c+d)的情况
            int c = 0, d = 0;
            for (int i = 0; i < 4; i++) {
                if (!vis[i] && flag == 2) {
                    // 没有被选
                    c = nums[i];
                    flag--;
                } else if (!vis[i] && flag == 1) {
                    d = nums[i];
                }
            }
            // 还原
            flag = 2;
            // 下面注销的都是不影响的
            //ans = ans + (c + d);
            //max = ans > max && ans <= 24 ? ans : max;
            //ans = ans - (c + d);
            //ans = ans - (c + d);
            //max = ans > max && ans <= 24 ? ans : max;
            //ans = ans + (c + d);
            ans = ans * (c + d);
            max = ans > max && ans <= 24 ? ans : max;
            ans = ans / (c + d);
            if (ans % (c + d) == 0) {
                ans = ans / (c + d);
                max = ans > max && ans <= 24 ? ans : max;
                ans = ans * (c + d);
            }


            //ans = ans + (c - d);
            //max = ans > max && ans <= 24 ? ans : max;
            //ans = ans - (c - d);
            //ans = ans - (c - d);
            //max = ans > max && ans <= 24 ? ans : max;
            //ans = ans + (c - d);
            if (c != d) {
                ans = ans * (c - d);
                max = ans > max && ans <= 24 ? ans : max;
                ans = ans / (c - d);
                if (ans % (c - d) == 0) {
                    ans = ans / (c - d);
                    max = ans > max && ans <= 24 ? ans : max;
                    ans = ans * (c - d);
                }
            }


            ans = ans + (c * d);
            max = ans > max && ans <= 24 ? ans : max;
            ans = ans - (c * d);
            ans = ans - (c * d);
            max = ans > max && ans <= 24 ? ans : max;
            ans = ans + (c * d);
            //ans = ans * (c * d);
            //max = ans > max && ans <= 24 ? ans : max;
            //ans = ans / (c * d);
            if (ans % (c * d) == 0) {
                ans = ans / (c * d);
                max = ans > max && ans <= 24 ? ans : max;
                ans = ans * (c * d);
            }


            if (c % d == 0) {
                ans = ans + (c / d);
                max = ans > max && ans <= 24 ? ans : max;
                ans = ans - (c / d);
                ans = ans - (c / d);
                max = ans > max && ans <= 24 ? ans : max;
                ans = ans + (c / d);
                //ans = ans * (c / d);
                //max = ans > max && ans <= 24 ? ans : max;
                //ans = ans / (c / d);
                if (ans % (c / d) == 0) {
                    ans = ans / (c / d);
                    max = ans > max && ans <= 24 ? ans : max;
                    ans = ans * (c / d);
                }
            }
        }
        for (int i = 0; i < 4; i++) {
            if (vis[i]) {
                // 选了
                continue;
            }
            // 选第i个
            vis[i] = true;
            // 之前的跟现在这个进行加减乘除
            // 加
            ans += nums[i];
            getAns(vis, nums, flag + 1, ans);
            ans -= nums[i];
            // 减
            ans -= nums[i];
            getAns(vis, nums, flag + 1, ans);
            ans += nums[i];
            // 乘
            ans *= nums[i];
            getAns(vis, nums, flag + 1, ans);
            ans /= nums[i];
            // 除
            if (ans % nums[i] == 0) {
                ans /= nums[i];
                getAns(vis, nums, flag + 1, ans);
                ans *= nums[i];
            }
            vis[i] = false;
        }
    }
}
  • 总结:题目还是比较有意思的,代码虽然有点长,但是大多数是重复的部分。
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值