【国科大卜算】Counterfeit Dollar

Counterfeit Dollar

文章目录

  • Counterfeit Dollar
    • Problem description
    • Input
    • Output
    • Sample
    • My understand

Problem 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 Output
1
ABCD EFGH even
ABCI EFJK up
ABIJ EFGH even
K is the counterfeit coin and it is light.

My understand

在天平中只有唯一假币的情况下,假币就是所有硬币中唯一那个重的或者唯一那个轻的,所以会出现以下情况:

  1. 天平平衡,天平中所有的硬币都为真
  2. 天平倾斜,天平外所有的硬币都为真。若左端在上方,则标记为轻,右端在下方,则标记为重。反之亦然。
  3. 因为所有硬币中唯一那个重的或者唯一那个轻的,所以出现轻的有出现重的必然是真币
//
// Created by Gowi on 2023/9/15.
//

#include 
#include 
#include 

using namespace std;

bool isSubStr(string str, char c) {
    for (int i = 0; i < str.length(); ++i) {
        if (c == str[i]) {
            return true;
        }
    }
    return false;
}

int main() {
    int n;
    cin >> n;
    while (n--) {
        string str[3][3] = {};
        int l = 'L' - 'A' + 1;
        int weight['L' - 'A' + 1] = {0};
        //1为真,-1为轻,2为重
        for (int i = 0; i < 3; ++i) {
            for (int j = 0; j < 3; ++j) {
                cin >> str[i][j];
            }
        }
        for (int i = 0; i < 3; ++i) {
            if (str[i][2] == "even") {
                //天平两侧一定为真
                for (int j = 0; j < 2; ++j) {
                    for (int k = 0; k < str[i][j].length(); ++k) {
                        weight[str[i][j][k] - 'A'] = 1;
                    }
                }
            } else if (str[i][2] == "up") {
                //其他一定为真
                for (char c = 'A'; c <= 'L'; c++) {
                    if (!isSubStr(str[i][0], c) && !isSubStr(str[i][1], c) && weight[c - 'A'] != 1) {
                        weight[c - 'A'] = 1;
                    }
                }
                //左侧重,标记为2
                for (int j = 0; j < str[i][0].length(); ++j) {
                    if (weight[str[i][0][j] - 'A'] == -1) {
                        weight[str[i][0][j] - 'A'] = 1;
                    }
                    if (weight[str[i][0][j] - 'A'] != 1) {
                        weight[str[i][0][j] - 'A'] = 2;
                    }
                }
                //右侧轻,标记为-1
                for (int j = 0; j < str[i][1].length(); ++j) {
                    if (weight[str[i][1][j] - 'A'] == 2) {
                        weight[str[i][1][j] - 'A'] = 1;
                    }
                    if (weight[str[i][1][j] - 'A'] != 1) {
                        weight[str[i][1][j] - 'A'] = -1;
                    }
                }
            } else {
                //其他一定为真
                for (int j = 0; j < 2; ++j) {
                    for (char c = 'A'; c <= 'L'; c++) {
                        if (!isSubStr(str[i][0], c) && !isSubStr(str[i][1], c) && weight[c - 'A'] != 1) {
                            weight[c - 'A'] = 1;
                        }
                    }
                }
                //右侧重,标记为2
                for (int j = 0; j < str[i][1].length(); ++j) {
                    if (weight[str[i][1][j] - 'A'] == -1) {
                        weight[str[i][1][j] - 'A'] = 1;
                    }
                    if (weight[str[i][1][j] - 'A'] != 1) {
                        weight[str[i][1][j] - 'A'] = 2;
                    }
                }
                //左侧轻,标记为-1
                for (int j = 0; j < str[i][1].length(); ++j) {
                    if (weight[str[i][0][j] - 'A'] == 2) {
                        weight[str[i][0][j] - 'A'] = 1;
                    }
                    if (weight[str[i][0][j] - 'A'] != 1) {
                        weight[str[i][0][j] - 'A'] = -1;
                    }
                }
            }
        }
        char c = 'A';
        for (int i = 0; i < l; ++i) {
            if (weight[i] == 0) {
                weight[i] = 1;
            }
            if (weight[i]==-1){
                printf("%c is the counterfeit coin and it is light.",c);
            }
            if (weight[i]==2){
                printf("%c is the counterfeit coin and it is heavy.",c);
            }
            c++;
        }
        cout << endl;
    }
}

你可能感兴趣的:(国科大,算法,算法)