poj1013解题报告

Counterfeit Dollar
Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 29060Accepted: 9114

Description

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
< fstream >

using namespace std;
int main()
{

// ifstream cin(
" in.txt " );
// ofstream cout(
" out.txt " );
int n;
cin
>> n;
while (n -- )
{
char left[ 3 ][ 8 ];
char right[ 3 ][ 8 ];
int state[ 3 ];
int mark[ 14 ]; // 标记,1为正常,0为没有,2为存在
memset(mark, 0 , 14 * sizeof ( int ));
int i = 0 ;
for (i = 0 ;i < 3 ;i ++ )
{
char sta[ 5 ];
int k = 0 ;
cin
>> left[i] >> right[i] >> sta;
int li = strlen(left[i]);
int ri = strlen(right[i]);
if (sta[ 0 ] == ' e ' )
{
state[i]
= 0 ;
for ( k = 0 ;k < li;k ++ ) mark[left[i][k] - ' A ' ] = 1 ;
for ( k = 0 ;k < ri;k ++ ) mark[right[i][k] - ' A ' ] = 1 ;
}
else if (sta[ 0 ] == ' d ' )
{
state[i]
= 1 ;
for ( k = 0 ;k < li;k ++ ) mark[left[i][k] - ' A ' ] = 2 ;
for ( k = 0 ;k < ri;k ++ ) mark[right[i][k] - ' A ' ] = 2 ;

}
else
{
state[i]
=- 1 ;
for ( k = 0 ;k < li;k ++ ) mark[left[i][k] - ' A ' ] = 2 ;
for ( k = 0 ;k < ri;k ++ ) mark[right[i][k] - ' A ' ] = 2 ;

}
}
for ( int r = 0 ;r < 14 ;r ++ )
{
if (mark[r] == 2 )
{
char Letter = r + ' A ' ;
bool success = true ;
// 先假设为重
for (i = 0 ;i < 3 && success;i ++ )
{
int t = 0 ;
int tli = strlen(left[i]);
bool isInL = false ;
int tri = strlen(right[i]);
bool isInR = false ;
for (t = 0 ;t < tli;t ++ )
{
if (left[i][t] == Letter)
{
isInL
= true ;
if (state[i] !=- 1 )
{
success
= false ;
break ;
}
if (state[i] ==- 1 )
{
break ;
}
}
}
for (t = 0 ;t < tri;t ++ )
{
if (right[i][t] == Letter)
{
isInR
= true ;
if (state[i] != 1 )
{
success
= false ;
break ;
}
if (state[i] == 1 )
{
break ;
}
}
}
if ( ! isInL && ! isInR && state[i] != 0 ) success = false ;

}
if (success)
{
cout
<< Letter << " is the counterfeit coin and it is heavy. " << " \n " ;
break ;
}
// 假设为轻
success = true ;
for (i = 0 ;i < 3 && success;i ++ )
{
int t = 0 ;
int tli = strlen(left[i]);
bool isInL = false ;
int tri = strlen(right[i]);
bool isInR = false ;
for (t = 0 ;t < tli;t ++ )
{
if (left[i][t] == Letter)
{
isInL
= true ;
if (state[i] != 1 )
{
success
= false ;
break ;
}
if (state[i] == 1 )
{
break ;
}
}
}
for (t = 0 ;t < tri;t ++ )
{
if (right[i][t] == Letter)
{
isInR
= true ;
if (state[i] !=- 1 )
{
success
= false ;
break ;
}
if (state[i] ==- 1 )
{
break ;
}
}
}
if ( ! isInL && ! isInR && state[i] != 0 ) success = false ;

}
if (success)
{
cout
<< Letter << " is the counterfeit coin and it is light. " << " \n " ;
break ;
}
}
}
}
}

转载于:https://www.cnblogs.com/easyFancy/archive/2011/06/07/2074225.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值
>