POJ1013-Counterfeit Dollar(枚举)

POJ1013-Counterfeit Dollar(枚举)

题目链接
题目大意

12枚硬币,其中有11枚真币和1枚假币,真币和假币重量不同,但不知道是真币重还是假币重,现在,用一架天平称了这些硬币三次,给你称量的结果,请你找出假币,并且确定假币是重还是轻。

Sample Input

1 
ABCD EFGH even 
ABCI EFJK up 
ABIJ EFGH even 

Sample Output

K is the counterfeit coin and it is light. 

解题思路

直接枚举12个字母:

  • 每个字母假设它是假币,且枚举它是重或者轻,也就是枚举12 * 2 = 24次,每次都调用isFake函数,如果满足条件,就输出对应信息;
  • isFake函数实现: 要分两种情况 ①假设此时假币c的,即light = true,则通过称量结果来判断这样是不是成立,判断要分三种情况,(1). 假如res = up,则表示左边重,右边轻,则在右边一定要能找到c,否则返回false(2). 假如res = even,则在左右两边都不能找到c,否则返回false(3). 如果res = down,则左边轻,右边重,则在左边一定要能找到c,否则返回false;②第二种情况,就是light = false,我们可以一开始对调字符串即可,过程同理;

Java

import java.io.BufferedInputStream;
import java.util.Scanner;

public class Main {

    static String[] left, right, result;

    static boolean isFake(char c, boolean light) {
        for (int i = 0; i < 3; i++) {
            String L, R;
            if (light) {
                L = left[i];
                R = right[i];
            } else {
                L = right[i];
                R = left[i];
            }
            //judge
            if (result[i].charAt(0) == 'u') {
                if (!R.contains(String.valueOf(c)))
                    return false;
            } else if (result[i].charAt(0) == 'e') {
                if (L.contains(String.valueOf(c)) || R.contains(String.valueOf(c)))
                    return false;
            } else {
                if (!L.contains(String.valueOf(c)))
                    return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        Scanner cin = new Scanner(new BufferedInputStream(System.in));
        int T = cin.nextInt();
        for(int t = 0; t < T; t++){
            left = new String[3];
            right = new String[3];
            result = new String[3];
            for (int i = 0; i < 3; i++) {
                left[i] = cin.next();
                right[i] = cin.next();
                result[i] = cin.next();
            }
            for (char c = 'A'; c <= 'L'; c++) {
                if (isFake(c, true)) {
                    System.out.println(c + " is the counterfeit coin and it is light. ");
                    break;
                } else if (isFake(c, false)) {
                    System.out.println(c + " is the counterfeit coin and it is heavy. ");
                    break;
                }
            }
        }
    }
}

C++

#include <iostream>
#include <stdio.h>
#include <string.h>

char left[3][7], right[3][7], result[3][7];

bool isFake(char c, bool light){ 
    for(int i = 0; i < 3; i++){ 
        char *pL,*pR;
        if(light){ 
            pL = left[i];
            pR = right[i];
        }else{ 
            pL = right[i];
            pR = left[i];
        }
        //assume light is true, judge
        switch(result[i][0]){ 
            case 'u': 
                if(strchr(pR, c) == NULL)
                    return false;
                break;
            case 'e':  // equals, --> c
                if(strchr(pL, c) || strchr(pR, c))
                    return false;
                break;
            case 'd':
                if(strchr(pL, c) == NULL)
                    return false;
                break;
        }
    }    
    return true;
}

int main(int argc, char const **argv)
{
    //freopen("in.txt", "r", stdin);
    int T;
    for(std::cin >> T; T--; ){ 
        for(int i = 0; i < 3; i++)
            std::cin>> left[i] >> right[i] >> result[i];
        for(char c = 'A'; c <= 'L'; c++){ 
            if(isFake(c, true)){
                std::cout << c <<" is the counterfeit coin and it is light. "<< std::endl;
                break;
            }else if(isFake(c, false)){ 
                std::cout << c <<" is the counterfeit coin and it is heavy. "<< std::endl;
                break;
            }
        }
    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值