poj 1013

来源:互联网 发布:手机淘宝差评改好评 编辑:程序博客网 时间:2024/05/16 07:15

//
解题思路:
总体就是枚举的思路,因为题目中规定了是A-L的12个字符,我们就枚举A-L,对于枚举的每一个字符c,也就是c是假币有两个情况,c是轻的假币或者, c是重的假币。
以上我们可以写出for(char c = A ~ L),然后我们用IsFake()函数来判断,注意IsFake()中的light 参数,如果light == true表示验证c是假的轻币的可能,如果light == false表示验证c是假的重币的可能。
strchar(str,’c’) == null 表示c不在str中。

#include <iostream>#include <cstring>#include <cstdlib>using namespace std;char Left[3][7];char Right[3][7];char Result[3][7];bool IsFake(char c, bool light){    //light 为真表示假币为轻,否则表示表示假币为重    for(int i = 0; i < 3; i++){        char *pLeft, *pRight;//指向天平两边的字符串        if(light) {            pLeft = Left[i];            pRight = Right[i];        }        else {//如果假设硬币是重的,则把称量左右交换,就是main中 else IsFake(...)那种            pLeft = Right[i];            pRight = Left[i];        }        //对每个c进行三组的比较,如果三组result都满足,则最后return true,表示此时的c是真的假币        switch(Result[i][0]) {//天平右边的情况            case 'u'://up表示c在右边.                    if(strchr(pRight,c) == NULL)//如果c不在右边                        return false;                    break;            case 'e':                    if(strchr(pLeft,c) || strchr(pRight,c))//如果轻的c在左边或者右边但是是"even"天平右边的情况                        return false;                    break;            case 'd':if(strchr(pLeft,c) == NULL)//如果c不在左边                        return false;                    break;        }    }    return true;}int main(){    int t;    cin >> t;    while(t--) {        for(int i = 0; i < 3; i++) cin >> Left[i] >> Right[i] >> Result[i];        for(char c = 'A'; c <= 'L'; c++) {            if(IsFake(c,true)) {                cout << c << " is the counterfeit coin and it is light." << endl;                break;            }            else if(IsFake(c,false)) {                cout << c << " is the counterfeit coin and it is heavy." << endl;                break;            }        }    }    // system("pause");    return 0;}
0 0