【九度】题目1150:Counterfeit Dollar

来源:互联网 发布:天注定 知乎 编辑:程序博客网 时间:2024/06/09 23:13
题目地址:http://ac.jobdu.com/problem.php?pid=1150
题目描述:

    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.

输入:

    For each case, 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.

输出:

    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.

样例输入:
1ABCD EFGH even ABCI EFJK up ABIJ EFGH even 
样例输出:
K is the counterfeit coin and it is light.
来源:
2010年北京大学计算机研究生机试真题
区分假币的问题,和题目1486:False coin思路一样。
1、如果出现在等号两边,说明一定为真币。
2、如果出现在大于或者小于号,相应的值递加或者递减。
3、如果某个值最后的结果和不等号出现的次数相等,说明该币为假币。
C++ AC
#include <stdio.h>#include <string.h>int arrayA[13];int arrayB[13];char array[13] = "ABCDEFGHIJKL";int n,i,j,k;int abs(int x){    return x < 0 ? -x : x;}void initArray(){    for(i = 0; i < 13; i++){        arrayA[i] = 0;        arrayB[i] = 1;    }}int main(){    while(scanf("%d",&n) != EOF){        char left[15],right[15],result[15];        for(i = 0; i < n ; i++){            int num = 0;            initArray();            for(j = 0; j < 3; j++){                scanf("%s %s %s",left,right,result);                int len1 = strlen(left);                int len2 = strlen(right);                if(strcmp(result,"even")==0){                    for(k = 0; k < len1; k++){                        arrayB[left[k] - 'A'] = 0;                    }                    for(k = 0; k < len2; k++){                        arrayB[right[k] - 'A'] = 0;                    }                }else if(strcmp(result,"up")==0){                    num++;                    for(k = 0; k < len1; k++){                        arrayA[left[k] - 'A']--;                    }                    for(k = 0; k < len2; k++){                        arrayA[right[k] - 'A']++;                    }                }else if(strcmp(result,"down")==0){                    num++;                    for(k = 0; k < len1; k++){                        arrayA[left[k] - 'A']++;                    }                    for(k = 0; k < len2; k++){                        arrayA[right[k] - 'A']--;                    }                }            }            int maxId = 0;            for(j = 0; j < 13; j++){                if(arrayB[j] == 1 && abs(arrayA[j]) == num){                    maxId = j;                }            }            char c = array[maxId];            if (arrayA[maxId] < 0) {                printf("%c is the counterfeit coin and it is heavy.\n",c);            }else {                printf("%c is the counterfeit coin and it is light.\n",c);            }        }    }    return 0;}/**************************************************************    Problem: 1150    User: wangzhenqing    Language: C++    Result: Accepted    Time:0 ms    Memory:1020 kb****************************************************************/

Java AC

import java.util.Arrays;import java.util.Scanner;  public class Main {     /*     * 1150     */    public static void main(String[] args) throws  Exception {        Scanner scanner = new Scanner(System.in);        while (scanner.hasNext()) {            int n = scanner.nextInt();//          ABCDEFGHIJKL            for (int m = 0; m < n; m++) {                int arrayA[] = new int[12];                int arrayB[] = new int[12];                Arrays.fill(arrayB, 1);                int num = 0;                for (int i = 0; i < 3; i++) {                    String left = scanner.next();                    String right = scanner.next();                    String result = scanner.next();                    int len1 = left.length();                    int len2 = right.length();                    if (result.equals("even")) {                        for (int j = 0; j < len1; j++) {                            arrayB[left.charAt(j)- 'A'] = 0;                        }                        for (int j = 0; j < len2; j++) {                            arrayB[right.charAt(j)-'A'] = 0;                        }                    }else if (result.equals("up")){                        num++;                        for (int j = 0; j < len1; j++) {                            arrayA[left.charAt(j)- 'A']--;                        }                        for (int j = 0; j < right.length(); j++) {                            arrayA[right.charAt(j)-'A']++;                        }                    }else if (result.equals("down")){                        num++;                        for (int j = 0; j < left.length(); j++) {                            arrayA[left.charAt(j)- 'A']++;                        }                        for (int j = 0; j < right.length(); j++) {                            arrayA[right.charAt(j)-'A']--;                        }                    }                }                                  int maxId = 0;                int k = 0;                while (k < 12) {                    if (arrayB[k] == 1 && Math.abs(arrayA[k]) == num) {                        maxId = k;                    }                    k++;                }                char c = (char) (maxId+'A');                if (arrayA[maxId] < 0) {                    System.out.println( c  +" is the counterfeit coin and it is heavy.");                }else {                    System.out.println( c +" is the counterfeit coin and it is light.");                }            }        }    }} /**************************************************************    Problem: 1150    User: wzqwsrf    Language: Java    Result: Accepted    Time:80 ms    Memory:15488 kb****************************************************************/
0 0