硬币称重问题之POJ1013-Counterfeit Dollar
来源:互联网 发布:Linux ppc64 编辑:程序博客网 时间:2024/05/20 06:26
Counterfeit Dollar
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 36491 Accepted: 11667
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.
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>using namespace std;#define COINSNUM 12int main(){char str[COINSNUM+1]="ABCDEFGHIJKL";int value[COINSNUM],n,i=0,j=0,k=0;char letters[3][COINSNUM];bool flags[COINSNUM];cin>>n;for(i=0;i<n;i++){for(j=0;j<COINSNUM;j++){flags[j]=true;value[j]=0;}for(j=0;j<3;j++){for(k=0;k<3;k++)cin>>letters[k];int len1=strlen(letters[0]);int len2=strlen(letters[1]);if(strcmp(letters[2],"even")==0){for(k=0;k<len1;k++){flags[letters[0][k]-'A']=false;value[letters[0][k]-'A']=0;}for(k=0;k<len2;k++){flags[letters[1][k]-'A']=false;value[letters[1][k]-'A']=0;}}else if(strcmp(letters[2],"up")==0){for(k=0;k<len1;k++){if(flags[letters[0][k]-'A']==true){value[letters[0][k]-'A']++;}}for(k=0;k<len2;k++){if(flags[letters[1][k]-'A']==true){value[letters[1][k]-'A']--;}}}else if(strcmp(letters[2],"down")==0){for(k=0;k<len1;k++){if(flags[letters[0][k]-'A']==true){value[letters[0][k]-'A']--;}}for(k=0;k<len2;k++){if(flags[letters[1][k]-'A']==true){value[letters[1][k]-'A']++;}}}}int max=0,maxIndex=0;for(j=0;j<COINSNUM;j++){if(abs(value[j])>max){max=abs(value[j]);maxIndex=j;}}cout<<str[maxIndex]<<" is the counterfeit coin and it is ";if(value[maxIndex]>0)cout<<"heavy."<<endl;else if(value[maxIndex]<0)cout<<"light."<<endl;}}
- 硬币称重问题之POJ1013-Counterfeit Dollar
- poj1013 Counterfeit Dollar 称硬币
- POJ1013 Counterfeit Dollar
- poj1013--Counterfeit Dollar
- poj1013,Counterfeit Dollar
- POJ1013 Counterfeit Dollar
- Counterfeit Dollar(POJ1013
- POJ1013 Counterfeit Dollar(技巧)
- poj1013 Counterfeit Dollar
- POJ1013 Counterfeit Dollar
- poj1013——Counterfeit Dollar
- POJ1013 Counterfeit Dollar 解题报告
- Counterfeit Dollar(poj1013暴力枚举)
- POJ1013——Counterfeit Dollar
- POJ1013-水模拟-Counterfeit Dollar
- poj1013-Counterfeit Dollar(简单逻辑题)
- POJ 1013 Counterfeit Dollar 砝码称重
- Counterfeit Dollar HDU1482题 FZU1003题 POJ1013题
- Slope One算法
- error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
- in 操作符
- Android仿QQ微信开场导航以及登陆界面
- Database system concept笔记(2)
- 硬币称重问题之POJ1013-Counterfeit Dollar
- Davinci开发板DM368 nandwrite.c简要分析
- dsfsdfsd
- 设置 li自适应宽度
- poj 1200字符串hash
- NAT原理与NAT穿越
- OpenStack unit tests 环境调试
- JS远程获取网页源代码实例
- List集合泛型的应用和遍历