POJ 1013 Counterfeit Dollar

Counterfeit Dollar
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 36206   Accepted: 11561

Descriptionapp

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.

Inputide

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.

Outputui

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 Inputspa

1 
ABCD EFGH even 
ABCI EFJK up 
ABIJ EFGH even 

Sample Outputcode

K is the counterfeit coin and it is light. 
題目大意:有12枚硬幣,裏面有一個是假的,稱三次,判斷哪枚是假的,而且判斷假的比真的輕仍是重。
解題方法:直接枚舉,總共有24中狀況,即每一枚硬幣都有可能比真的輕或者重,枚舉這24種狀況,便可得出答案。
#include <stdio.h>
#include <string.h>

int main()
{
    char str1[10], str2[10], balance[10];
    int nCase, ans[25];//ans用於記錄每種狀況知足的次數有多少次
    scanf("%d", &nCase);
    while(nCase--)
    {
        memset(ans, 0, sizeof(ans));
        for (int i = 0; i < 3; i++)
        {
            scanf("%s%s%s", str1, str2, balance);
            //j等於0到11依次表示A-L中假幣輕,j等於12到23依次表示A-L中假幣重
            for (int j = 0; j < 24; j++)
            {
                switch(balance[0])
                {
                case 'e':
                    {
                        bool flag = true;
                        //若是假幣輕則在兩邊都應該找不到該硬幣
                        if (j < 12)
                        {
                            for (int k = 0; k < strlen(str1); k++)
                            {
                                if (str1[k] == 'A' + j)
                                {
                                    flag = false;
                                }
                            }
                            for (int k = 0; k < strlen(str2); k++)
                            {
                                if (str2[k] == 'A' + j)
                                {
                                    flag = false;
                                }
                            }
                        }
                        //若是假幣重則在兩邊都應該找不到該硬幣
                        else
                        {
                            for (int k = 0; k < strlen(str1); k++)
                            {
                                if (str1[k] == 'A' + j - 12)
                                {
                                    flag = false;
                                }
                            }
                            for (int k = 0; k < strlen(str2); k++)
                            {
                                if (str2[k] == 'A' + j - 12)
                                {
                                    flag = false;
                                }
                            }
                        }
                        //若是兩邊都沒找到,則說明知足條件
                        if (flag)
                        {
                            ans[j]++;
                        }
                    }
                    break;
                case 'u':
                    {
                        bool flag = false;
                        //若是左邊重則輕的假幣放在右邊
                        if (j < 12)
                        {
                            for (int k = 0; k < strlen(str2); k++)
                            {
                                if (str2[k] == 'A' + j)
                                {
                                    flag = true;
                                }
                            }
                        }
                        //若是左邊重則重的假幣放在左邊
                        else
                        {
                            for (int k = 0; k < strlen(str1); k++)
                            {
                                if (str1[k] == 'A' + j - 12)
                                {
                                    flag = true;
                                }
                            }
                        }
                        if (flag)
                        {
                            ans[j]++;
                        }
                    }
                    break;
                case 'd':
                    {
                        bool flag = false;
                        //若是右邊重,則輕的假幣放在左邊
                        if (j < 12)
                        {
                            for (int k = 0; k < strlen(str1); k++)
                            {
                                if (str1[k] == 'A' + j)
                                {
                                    flag = true;
                                }
                            }
                        }
                        //若是右邊重,則重的假幣放在右邊
                        else
                        {
                            for (int k = 0; k < strlen(str2); k++)
                            {
                                if (str2[k] == 'A' + j - 12)
                                {
                                    flag = true;
                                }
                            }
                        }
                        if (flag)
                        {
                            ans[j]++;
                        }
                    }
                    break;
                }
            }
        }
        for (int i = 0; i < 24; i++)
        {
            //ans[i] == 3說明這種狀況知足三次稱量的結果
            if (ans[i] == 3)
            {
                //假幣比真幣輕
                if (i < 12)
                {
                    printf("%c is the counterfeit coin and it is light.\n", i + 'A');
                }
                //假幣比真幣重
                else
                {
                    printf("%c is the counterfeit coin and it is heavy.\n", i - 12 + 'A');
                }
                break;
            }
        }
    }
    return 0;
}
相關文章
相關標籤/搜索