做题笔记(POJ P1013 称硬币)
作者:互联网
题目原文
传送门 POJ P1013
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.
思路分析
乍一看这个题目很难,仔细一看其实也很难
我们来分析一下题目
题目大意是有12枚硬币,编号A到L,其中有一枚假的,重量和真币不一样,真币重量都是一样的,称量三次,给出了称量方法和结果,问哪一枚是假的,比真币轻还是重
搞清楚意思以后来逐步分析
- 首先是解决数据读取的问题,看样例可以知道,一组数据三行,每行三个字符串
因为char数组是8个字节,十二个硬币最多在天平左右各放6个,也就是一个字符串最多6个字节,所以我们可以开三个char类型的二维数组,循环读取每行的三个字符串 - 搞定读入,来看看具体怎么操作,因为要找出假币,然而称量数据没有规律,所以只能枚举每一枚硬币,看看假如它是真(假)的对于数据是否成立,但是题目又要求判断它是轻的还是重的~~(分别称一下12个硬币不就行了吗)~~,那么我们这样子做:枚举每一枚硬币,假设它是假的,看它如果轻是否成立,如果重是否成立,都不成立continue
- 最后就是写出代码,完善细节
我的代码如下
#include<iostream>
#include<cstring>
using namespace std;
char l[3][7]; //天平左边的硬币
char r[3][7]; //天平右边的硬币
char result[3][7];//称量结果
char c;
bool light; //light为真,硬币轻,light为假,硬币重
bool check(char c,bool light){ //check函数用来检查假设是否成立,也是程序主要的部分
for(int i=0;i<3;i++){
char *pl,*pr; //两个指针
if(light){ //如果假设轻,那么pl指左边,pr指右边
pl=l[i];
pr=r[i];
}
else{ //如果假设轻,那么pl指右边,pr指左边(为什么对调看下面的步骤)
pl=r[i];
pr=l[i];
}
switch(result[i][0]){ //称量结果看结果的第一个字符就好
case 'u': //右边up右边轻,如果假设c为轻的,那么c在右边,如果假设c为重的,c在左边
if(strchr(pr,c)==NULL)
return false;
break;
case 'e': //两边even一样重,不在左也不在右
if(strchr(pr,c)||strchr(pl,c))
return false;
break;
case 'd': //同up
if(strchr(pl,c)==NULL)
return false;
break;
}
}
return true;
}
int main(){
int t;
cin>>t; //输入有几组测试数据
while(t--){ //依据有几组测试数据来循环,因为不知道循环次数就用while(t--)这样子
for(int i=0;i<3;i++){
cin>>l[i]>>r[i]>>result[i]; //循环读入
}
for(char i='A';i<='L';i++){
if(check(i,true)){ //假设i为假币且更轻
cout<<i<<" "<<"is the counterfeit coin and it is light. ";
break;
}
else if(check(i,false)){ //假设i为假币且更重
cout<<i<<" "<<"is the counterfeit coin and it is heavy. ";
break;
}
}
}
return 0;
}
代码里有一个strchr函数,特别介绍一下
strchr函数的函数库头文件为#include < cstring >.
函数原型为:char * strchr(char * str,char character)
参数:str为一个字符串的指针,character为一个待查找字符
函数作用:查找字符串str中首次出现字符character的位置的指针。查找到返回位置指针,如果字符串中没有所要寻找的字符则返回NULL
END
标签:will,coin,硬币,coins,P1013,char,POJ,balance,Sally 来源: https://blog.csdn.net/sinat_41822854/article/details/99195879