poj 1013 模拟 天平问题

本文详细解析了天平问题恶心模拟算法的实现过程,包括输入数据处理、逻辑判断和输出结果展示,通过实例代码深入理解算法核心。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//============================================================================
//
// >     File        :    poj1013.cpp
// >     Author      :    flowertree
// >     Time        :    2015年12月22日
// >     Algorithm   :    天平问题 恶心模拟
//
//============================================================================

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
using namespace std;

#define MAX 12

bool flag[MAX];
int mabey[MAX];

int main()
{
	int m;
	cin >> m;
	while(m--)
	{
		char str1[7], str2[7], temp[10];
		int len;
		int Temp;
		memset(flag, false, sizeof(flag));
		memset(mabey, 0, sizeof(mabey));
		for(int i = 0; i < 3; i++)
		{
			scanf("%s%s%s", str1, str2, temp);
			len = strlen(str1);
			if(strcmp(temp, "even") == 0)
			{
				for(int i = 0; i < len; i++)
				{
					mabey[str1[i] - 'A'] = 0;
					flag[str1[i] - 'A'] = true;
					mabey[str2[i] - 'A'] = 0;
					flag[str2[i] - 'A'] = true;
				}
				continue;
			}
			else if(strcmp(temp, "up") == 0)
				Temp = -1;
			else if(strcmp(temp, "down") == 0)
				Temp = 1;
			bool mark[MAX];
			memset(mark, false, sizeof(mark));
			for(int i = 0; i < len; i++)
			{
				mark[str1[i] - 'A'] = true;
				mark[str2[i] - 'A'] = true;
				if(!flag[str1[i] - 'A'])
				{
					if(mabey[str1[i] - 'A'] == Temp)
					{
						mabey[str1[i] - 'A'] = 0;
						flag[str1[i] - 'A'] = true;
					}
					else
						mabey[str1[i] - 'A'] = -Temp;
				}
				if(!flag[str2[i] - 'A'])
				{
					if(mabey[str2[i] - 'A'] == -Temp)
					{
						mabey[str2[i] - 'A'] = 0;
						flag[str2[i] - 'A'] = true;
					}
					else
						mabey[str2[i] - 'A'] = Temp;
				}
			}
			for(int i = 0; i < MAX; i++)
			{
				if(!mark[i])
				{
					flag[i] = true;
					mabey[i] = 0;
				}
			}
		}
		for(int i = 0; i < 12; i++)
		{
			if(mabey[i] != 0)
			{
				if(mabey[i] == 1)
					cout << (char)(i + 'A') << " is the counterfeit coin and it is heavy." << endl;
				else if(mabey[i] == -1)
					cout << (char)(i + 'A') << " is the counterfeit coin and it is light." << endl;
				break;
			}
		}
	}
	system("pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值