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 Aidway at 2016-02-04 15:48:35 on Problem 1013
#include <iostream>
#include <string>
using namespace std;

int ans[30];

// 返回两堆硬币称重的结果 
string weight(string str1,string str2)
{
	int len = str1.length();
	int w1, w2;
	w1 = w2 = 0;
	for (int i=0; i<len; i++)
	{
		w1 += ans[str1[i]-'A'];
		w2 += ans[str2[i]-'A'];
	}
	if (w1 == w2)
	{
		return "even";
	}
	else if (w1 > w2)
	{
		return "up";
	}
	else
	{
		return "down";
	}
}

// 初始化数据ans 
void init()
{
	for (int i=0; i<30; i++)
	{
		ans[i] = 0;
	}
}

int main()
{
	int n;
	scanf("%d", &n);
	string left[3],right[3],result[3];
	
	while (n--)
	{	
		init();
		for (int i=0; i<3; i++)
		{
			cin >> left[i] >> right[i] >> result[i];	
			int len = left[i].length();
			for (int j=0; j<len; j++)
			{
				ans[right[i][j]-'A'] = 1;
				ans[left[i][j]-'A'] = 1;
			}
		}
		
		int num;
		string counterfeit ;  
		for (int i=0; i<12; i++)
		{
			// 对每个被称过的硬币 
			if (ans[i] > 0)
			{
				// 假设第i个硬币是假币,而且是轻的 
				ans[i] -= 1;
				int j;
				for (j=0; j<3; j++)
				{
					if (weight(left[j],right[j]) != result[j])
					{
						break;
					}
				}
				if (j >= 3)
				{
					counterfeit = "light";
					num = i;
					break;
				}
				// 假设第i个硬币是假币,而且是重的 
				ans[i]+=2;
				for (j=0; j<3; j++)
				{	
					if (weight(left[j],right[j]) != result[j])
					{
						break;
					}
				}
				if (j >= 3)
				{
					counterfeit = "heavy";
					num = i;
					break;
				}
				ans[i] -= 1;
			}
		}
		cout << char('A'+num) << " is the counterfeit coin and it is " << counterfeit << "." << endl;
	}
}

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