您的位置:首页 > Web前端

ZOJ1184 Counterfeit Dollar

2011-05-25 19:38 381 查看
这真是个让人蛋疼的题目:确定读入左边的字符有4个的,用

for(int jj=0;jj<4;jj++)
{
Coin[leftBalance[jj]-'A']=100;
Coin[rightBalance[jj]-'A']=100;
}


这样写是提交通不过的

for(int jj=0;jj<leftBalance.size();jj++)
{
Coin[leftBalance[jj]-'A']=100;
Coin[rightBalance[jj]-'A']=100;
}


这个才能通过

相当不理解啊,因为leftBalance.size()就是等于4的嘛!

Counterfeit Dollar

Time Limit: 1 Second Memory Limit: 32768 KB

Sally Jones has a dozen Voyageur silver dollars. However, only eleven of the coins are true silver dollars; one coin is counterfeit even though its color and size make it indistinguishable from the real silver dollars. The counterfeit coin has a different weight from the other coins but Sally does not know if it is heavier or lighter than the real coins.

Happily, Sally has a friend who loans her a very accurate balance scale. The friend will permit Sally three weighings to find the counterfeit coin. For instance, if Sally weighs two coins against each other and the scales balance then she knows these two coins are true. Now if Sally weighs one of the true coins against a third coin and the scales do not balance then Sally knows the third coin is counterfeit and she can tell whether it is light or heavy depending on whether the balance on which it is placed goes up or down, respectively.

By choosing her weighings carefully, Sally is able to ensure that she will find the counterfeit coin with exactly three weighings.

Input

The first line of input is an integer n (n > 0) specifying the number of cases to follow. Each case consists of three lines of input, one for each weighing. Sally has identified each of the coins with the letters A-L. Information on a weighing will be given by two strings of letters and then one of the words ``up'', ``down'', or ``even''. The first string of letters will represent the coins on the left balance; the second string, the coins on the right balance. (Sally will always place the same number of coins on the right balance as on the left balance.) The word in the third position will tell whether the right side of the balance goes up, down, or remains even.

Output

For each case, the output will identify the counterfeit coin by its letter and tell whether it is heavy or light. The solution will always be uniquely determined.

Sample Input

1
ABCD EFGH even
ABCI EFJK up
ABIJ EFGH even

Sample Output

K is the counterfeit coin and it is light.

#include<iostream>
#include<string>
#include <math.h>
#include <stdlib.h>
using namespace std;
int main()
{
int cases;
cin>>cases;
while(cases--)
{
int Coin[15]={0}; //0表示未处理 1表示真 -1表示假
bool real[15] = {false};
string leftBalance; //为了方便参数的传递,特此修改成全局的变量
string rightBalance;
string status;
int banlanceNumber=0;
int unbanlanceNumber=0;
for(int ii=0;ii<3;ii++)
{
cin>>leftBalance>>rightBalance>>status;
int lc=0;
if(status=="even") //平衡
{
banlanceNumber++;
for(int jj=0;jj<leftBalance.size();jj++) { Coin[leftBalance[jj]-'A']=100; Coin[rightBalance[jj]-'A']=100; }
}
else if(status=="up") //右边轻
{
for(int jj=0;jj<leftBalance.size();jj++)
{
if(Coin[leftBalance[jj]-'A']!=100)
Coin[leftBalance[jj]-'A']--;
if(Coin[rightBalance[jj]-'A']!=100)
Coin[rightBalance[jj]-'A']++;
}
}
else//右边重
{
for(int jj=0;jj<leftBalance.size();jj++)
{
if(Coin[leftBalance[jj]-'A']!=100)
Coin[leftBalance[jj]-'A']++;
if(Coin[rightBalance[jj]-'A']!=100)
Coin[rightBalance[jj]-'A']--;
}
}
}
unbanlanceNumber=3-banlanceNumber; //不平衡数目
int Min=0,Max=0;
int minLoc=0,maxLoc=0;
for(int ii=0;ii<12;ii++)
{
if(Coin[ii]==100)
continue;
else if(Coin[ii] > Max)
{
Max = Coin[ii];
maxLoc =ii;
}
else if(Coin[ii] < Min)
{
Min = Coin[ii];
minLoc =ii;
}
}
char fakeCoin='0';
int isLight=0; //1表示假币轻了,0表示假币重了
if( Max == unbanlanceNumber )
{
fakeCoin = 'A'+maxLoc;
isLight = 1;
}
else if(Min == -unbanlanceNumber)
{
fakeCoin = 'A'+minLoc;
isLight = 0;
}
if(isLight)
cout<<fakeCoin<<" is the counterfeit coin and it is light."<<endl;
else
cout<<fakeCoin<<" is the counterfeit coin and it is heavy."<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: