问题 1498: [蓝桥杯][算法提高VIP]凶手(思维)

题目链接:点击这里

在这里插入图片描述
在这里插入图片描述

只有一半人说了真话,凶手只有一个。

一种思路是,枚举三个说真话的人,然后判断凶手是否唯一,最终按字母顺序依次输出。

逆向考虑更简单,假设每个人都可能是凶手,依次枚举,然后判断是否有三个命题为真。

#include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<stack>
#include<queue>
#include<map>
#include<set>

using namespace std;
typedef long long ll;
const int MOD = 10000007;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);
const int maxn = 10;
int a[10];

bool check()
{
	int cnt = 0;
	
	bool flaga = false;
	if(a[1]==0)				//A真
	{
		flaga = true;
		cnt++; 
	}
	
	bool flagb = false;
	if((a[1]==1&&a[3]==0)||(a[1]==0&&a[3]==1))		//B真
	{
		flagb = true;
		cnt++;	
	}
	
	bool flagf = false;
	if(a[6]==1)					//F真 
	{
		flagf = true;
		cnt++;
	}
	
	bool flagc = false;
	if(!flaga&&!flagb)			//C真 
	{
		flagc = true;
		cnt++;
	} 
	
	bool flagd = false;
	if(!flagc&&!flagf)		//D真
	{
		flagd = true;
		cnt++;
	}
	
	bool flage = false;
	if(flaga && flagd && !flagb && !flagc && !flagf)		//E真
	{
		flage = true;
		cnt++;
	}
	
	if(cnt==3)	return true;
	else	return false;
}

int main()
{
	for(int i = 1; i <= 6; ++i)
	{
		memset(a, 0, sizeof(a));
		a[i] = 1;
		
		if(check())
			printf("%c\n", 'A'+i-1);
	}
	
	return 0;
}
发布了748 篇原创文章 · 获赞 113 · 访问量 13万+

猜你喜欢

转载自blog.csdn.net/qq_42815188/article/details/104504885