蓝桥杯历届真题 #食堂(C++,Java)

这题没什么好说的

考虑所有情况然后写就完了

虽然赛场上 交完不知道答案(doge)

原题链接

cpp 复制代码
#include<iostream>

using namespace std;

int main() {
    int n;
    cin >> n;
    //能优先安排6人桌,要先安排6人桌
    //6人桌可以是2+2+2 或者 3+3 或者4+2
    //优先用3+3组合,因为3人寝只能凑6人桌
    //2+2+2 和 4+2 优先用哪个都一样
    //因为剩下奇数个2还是偶数个2都能去搭配
    while (n--) {
        int a2, a3, a4, b4, b6;
        cin >> a2 >> a3 >> a4 >> b4 >> b6;
        int res = 0;
        //先凑6人
        while (b6) {
            //3+3
            if (a3 / 2 > 0 && a3) {
                res += 6;
                a3 -= 2;
                b6 -= 1;
                continue;
            }
            //4+2(2+2+2也可以)
            if (a4 && a2) {
                res += 6;
                a4 -= 1;
                a2 -= 1;
                b6 -= 1;
                continue;
            }
            //最后凑2+2+2
            if (a2 / 3 != 0 && a2) {
                res += 6;
                a2 -= 3;
                b6 -= 1;
                continue;
            }
            //凑不出来6人就break
            //凑6人2+2+2 或者a3>=2 或者 1个4 1个2
            if ((a2 == 0 && a3 < 2) || (a2 < 3 && a3 < 2 && a4 == 0))break;
        }
        //凑4人
        while (b4 > 0) {
            //凑4人桌的优先级也是一样的(找不出来反例)
            if (a4) {
                a4 -= 1;
                res += 4;
                b4 -= 1;
                continue;
            }
            if (a2 / 2 != 0 && a2) {
                a2 -= 2;
                res += 4;
                b4 -= 1;
                continue;
            }
            //凑不出来4人就break
            //凑4人 2+2 或者 4
            if (a2 <= 1 && a4 == 0)break;
        }
        //最后盘算坐不满的情况
        //比如一个6人桌给两个人坐
        while (b6) {
            if (a2 && a3) {
                a2 -= 1;
                a3 -= 1;
                res += 5;
                b6 -= 1;
            }
            else if (a4) {
                a4 -= 1;
                b6 -= 1;
                res += 4;
            }
            else if (a3) {
                a3 -= 1;
                b6 -= 1;
                res += 3;
            }
            else if (a2) {
                a2 -= 1;
                b6 -= 1;
                res += 2;
            }
            else break;
        }
        while (b4) {
            if (a3 > 0) {
                b4 -= 1;
                a3 -= 1;
                res += 3;
            }
            else if (a2) {
                b4 -= 1;
                a2 -= 1;
                res += 2;
            }
            else break;
        }

        cout << res << endl;

    }
    return 0;
}
java 复制代码
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();
        while (n-- > 0) {
            int a2 = scanner.nextInt();
            int a3 = scanner.nextInt();
            int a4 = scanner.nextInt();
            int b4 = scanner.nextInt();
            int b6 = scanner.nextInt();

            int res = 0;
            // 先凑6人
            while (b6 > 0) {
                // 3+3
                if (a3 >= 2) {
                    res += 6;
                    a3 -= 2;
                    b6 -= 1;
                    continue;
                }
                // 4+2
                if (a4 > 0 && a2 > 0) {
                    res += 6;
                    a4 -= 1;
                    a2 -= 1;
                    b6 -= 1;
                    continue;
                }
                // 2+2+2
                if (a2 >= 3) {
                    res += 6;
                    a2 -= 3;
                    b6 -= 1;
                    continue;
                }
                // 凑不出来6人就break
                if ((a2 == 0 && a3 < 2) || (a2 < 3 && a3 < 2 && a4 == 0)) {
                    break;
                }
            }
            // 凑4人
            while (b4 > 0) {
                // 凑4人桌的优先级也是一样的(找不出来反例)
                if (a4 > 0) {
                    a4 -= 1;
                    res += 4;
                    b4 -= 1;
                    continue;
                }
                if (a2 >= 2) {
                    a2 -= 2;
                    res += 4;
                    b4 -= 1;
                    continue;
                }
                // 凑不出来4人就break
                if (a2 <= 1 && a4 == 0) {
                    break;
                }
            }
            // 最后盘算坐不满的情况
            while (b6 > 0) {
                if (a2 > 0 && a3 > 0) {
                    a2 -= 1;
                    a3 -= 1;
                    res += 5;
                    b6 -= 1;
                } else if (a4 > 0) {
                    a4 -= 1;
                    res += 4;
                    b6 -= 1;
                } else if (a3 > 0) {
                    a3 -= 1;
                    res += 3;
                    b6 -= 1;
                } else if (a2 > 0) {
                    a2 -= 1;
                    res += 2;
                    b6 -= 1;
                } else {
                    break;
                }
            }
            while (b4 > 0) {
                if (a3 > 0) {
                    b4 -= 1;
                    a3 -= 1;
                    res += 3;
                } else if (a2 > 0) {
                    b4 -= 1;
                    a2 -= 1;
                    res += 2;
                } else {
                    break;
                }
            }

            System.out.println(res);
        }

        scanner.close();
    }
}
相关推荐
键盘会跳舞9 分钟前
【C++多线程】死锁诊断工具实战:从 Windows 到 Linux 的全链路排查
linux·c++·windows·死锁
吴声子夜歌12 分钟前
Java扩展——OkHttp
java·开发语言·okhttp
卓怡学长19 分钟前
w210基于springboot反诈平台设计与实现
java·spring boot·spring·intellij-idea
萧瑟余晖28 分钟前
Java深入解析篇六十一之编译器API(javax.tools)详解
java·开发语言
HugoStudio_SWAN40 分钟前
洛谷 P10719 \[GESP202406 五级] 黑白格——暴力美学与图像处理的最小外接矩形
c++·图像处理·人工智能·学习·程序人生·算法·目标跟踪
jimy11 小时前
C++ 的 “移动语义”——Move Semantics
开发语言·c++
码云数智-园园1 小时前
C++ STL 容器怎么选?vector、list、map、unordered_map 优缺点对比
java·开发语言
en.en..1 小时前
竖线 |:管道、按位或、掩码组合
java·开发语言
程序猿乐锅2 小时前
【计算机组成原理 | 第六章】中央处理器 CPU
java·开发语言
大模型丫丫2 小时前
如何提升单体 Spring Boot 应用的并发数?
java·spring boot·后端