zoukankan      html  css  js  c++  java
  • poj1013

    一道简单题,思路如下:

    1、对每个硬币设三个bool位进行标识,分别为平衡even,重heavy,轻light;

    2、读入数据,对硬币状态进行标记,这里有一个细节是,如果天枰左偏或右偏,要将没有上天枰的硬币的even位都标记为真;

    3、如果硬币even位为真,或者硬币的heavy和light位同为真,那么这个硬币是真硬币,否则就是假硬币。

    题设中的“The solution will always be uniquely determined.”为这种解法提供了保证。

    #include <iostream>
    #include <string>
    using namespace std;
    
    struct Poss{
        bool even;
        bool heavy;
        bool light;
    };
    
    int main()
    {
        int n;
        cin >> n;
        while (n--){
            string left, right, result;
            Poss arr[12];
            memset(arr, 0, sizeof(arr));
            for (int i = 0; i < 3; i++){
                cin >> left >> right >> result;
                int len = left.length();
                if (result == "even"){
                    for (int i = 0; i < len; i++){
                        arr[left[i] - 'A'].even = true;
                        arr[right[i] - 'A'].even = true;
                    }
                }
                else{
                    bool on[12] = { false };
                    for (int i = 0; i < len; i++){
                        on[left[i] - 'A'] = true;
                        on[right[i] - 'A'] = true;
                    }
                    for (int i = 0; i < 12; i++){
                        if (!on[i]){
                            arr[i].even = true;
                        }
                    }
                    if (result == "up"){
                        for (int i = 0; i < len; i++){
                            arr[left[i] - 'A'].heavy = true;
                            arr[right[i] - 'A'].light = true;
                        }
                    }
                    else{
                        for (int i = 0; i < len; i++){
                            arr[right[i] - 'A'].heavy = true;
                            arr[left[i] - 'A'].light = true;
                        }
                    }
                }
            }
            for (int i = 0; i < 12; i++){
                if (arr[i].even || (arr[i].heavy && arr[i].light))
                    continue;
                if (arr[i].heavy){
                    cout << char('A' + i) << " is the counterfeit coin and it is heavy." << endl;
                    break;
                }
                else if (arr[i].light){
                    cout << char('A' + i) << " is the counterfeit coin and it is light." << endl;
                    break;
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    c#+oracle存储过程实现分页
    C#中调用Matlab程序
    Oracle 自定义TYPE 的几种用法(转)
    oracle嵌套表示例
    矩阵的秩及矩阵的广义逆
    矩阵的定义及其运算规则
    矩阵微分
    matlab中取模(mod)与取余(rem)的区别
    hog源码分析
    矩阵的转置、求逆及分块
  • 原文地址:https://www.cnblogs.com/caiminfeng/p/4868076.html
Copyright © 2011-2022 走看看