【白帽子红帽子问题】

文章描述了一个逻辑谜题,涉及五个人分别戴白帽子或红帽子,根据他们的陈述设计算法判断每个人帽子的颜色。使用编程语言展示了如何通过条件判断逐一确定每个人的帽子颜色。
摘要由CSDN通过智能技术生成

白帽子红帽子问题:厅内有 5 个人,他们均戴着帽子,有白帽子和红帽子。已知戴白帽子的说真话,戴红帽子的说假话,他们的说法如下:
甲:我看见一个戴白帽子的
乙:我没有看见戴红帽子的
丙:我看见一个戴白帽子的,但不是甲
丁:我没有看见戴白帽子的
戊:我的帽子和丙一样
请从他们各自提供的线索辨别谁戴白帽子,谁戴红帽子,需要利用蛮力法设计算法求解问题。

代码如下:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<istream>
#include<iomanip>
#include<ostream>
#include<list>
#include<vector>
#include<set>
#include<map>
#include<fstream>
#include<stack>
#include<ctime>
#include<deque>
#include<queue>
#include <sstream>
#include <numeric>

#pragma warning (disable:4996)

using namespace std;

int main()
{
    int c1, c2, c3, c4, c5;
    for (int a = 0; a <= 1; a++)
        for (int b = 0; b <= 1; b++)
            for (int c = 0; c <= 1; c++)
                for (int d = 0; d <= 1; d++)
                    for (int e = 0; e <= 1; e++)
                    {
                        c1 = ((b + c + d + e) == 1) == a;
                        c2 = ((a + c + d + e) == 4) == b;
                        c3 = ((b + d + e) == 1) == c;
                        c4 = ((a + b + c + e) == 0) == d;
                        c5 = (c == e) == e;
                        if (c1 && c2 && c3 && c4 && c5)
                        {
                            if (a == 1)
                                printf("甲戴帽子\n");
                            else
                                printf("甲戴红帽子\n");

                            if (b == 1)
                                printf("乙戴白帽子\n");
                            else
                                printf("乙戴红帽子\n");

                            if (c == 1)
                                printf("丙戴白帽子\n");
                            else
                                printf("丙戴红帽子\n");

                            if (d == 1)
                                printf("丁戴白帽子\n");
                            else
                                printf("丁戴红帽子\n");

                            if (e == 1)
                                printf("戊戴白帽子\n");
                            else
                                printf("戊戴红帽子\n");
                        }
                    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

武帝为此

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值