Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

简单暴力枚举

Posted by ZSTU_Time at 2015-03-05 09:07:18 on Problem 1013 and last updated at 2015-03-05 09:11:09
a数组记录天平结果,map记录字母的重量(0正常, 1重, -1轻),一个一个去试那三个天平

code:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
using namespace std;

string s1, s2, s3, s4, s5, s6, s7, s8, s9;
map<char, int> m;
int a[4];

void judge(void)	//0-even 1-up -1-down
{
	if (s3[0] == 'e')	a[1] = 0;
	else if (s3[0] == 'u')	a[1] = 1;
	else	a[1] = -1;
	if (s6[0] == 'e')	a[2] = 0;
	else if (s6[0] == 'u')	a[2] = 1;
	else	a[2] = -1;
	if (s9[0] == 'e')	a[3] = 0;
	else if (s9[0] == 'u')	a[3] = 1;
	else	a[3] = -1;
	//cout << a[1] << a[2] << a[3] << endl;
}

bool ok(void)
{
	int sum1 = 0, sum2 = 0;
	for (int i=0; s1[i]!='\0'; ++i)
	{
		sum1 += m[s1[i]];
	}
	for (int i=0; s2[i]!='\0'; ++i)
	{
		sum2 += m[s2[i]];
	}
	//cout << sum1 << '-' << sum2 << "->" << a[1] << endl;
	if (sum1 - sum2 != a[1])	return false;

	sum1 = 0;	sum2 = 0;
	for (int i=0; s4[i]!='\0'; ++i)
		sum1 += m[s4[i]];
	for (int i=0; s5[i]!='\0'; ++i)
	{
		sum2 += m[s5[i]];
	}
	//cout << sum1 << '-' << sum2 << "->" << a[1] << endl;
	if (sum1 - sum2 != a[2])	return false;

	sum1 = 0;	sum2 = 0;
	for (int i=0; s7[i]!='\0'; ++i)
		sum1 += m[s7[i]];
	for (int i=0; s8[i]!='\0'; ++i)
		sum2 += m[s8[i]];
	//cout << sum1 << '-' << sum2 << "->" << a[1] << endl;
	if (sum1 - sum2 != a[3])	return false;

	return true;
}

void work(void)
{
	judge ();
	for (int i=0; i<=11; ++i)
	{
		char ch = 'A' + i;

		m[ch] = -1;
		if (ok())
		{
			cout << ch << " is the counterfeit coin and it is light." << endl;
			break;
		}
		m[ch] = 1;
		if (ok())
		{
			cout << ch << " is the counterfeit coin and it is heavy." << endl;
			break;
		}
		m[ch] = 0;
	}
}

int main(void)
{
	//freopen ("C.in", "r", stdin);

	int t;
	cin >> t;
	while (t--)
	{
		for (int i=0; i<=11; ++i)	m['A' + i] = 0;
		cin >> s1 >> s2 >> s3;
		cin >> s4 >> s5 >> s6;
		cin >> s7 >> s8 >> s9;
		work ();
	}

	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator