POJ 1013 题解

来源:互联网 发布:免费的一级域名 编辑:程序博客网 时间:2024/06/07 09:39

转载请注明出处,http://blog.csdn.net/Bule_Zst/article/details/77154288


题目:http://poj.org/problem?id=1013

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

1 ABCD EFGH even ABCI EFJK up ABIJ EFGH even 

Sample Output

K is the counterfeit coin and it is light. 

题意:

一共12枚硬币,其中有一枚是假的,假币重量与真币不同,或大或小。

现给出三次天枰称量结果,求假币是哪一枚,以及它比真币重还是轻。


方法:

第一种

首先记录输入,然后把称量结果为even的币都排除掉,之后开一个12个元素的整形数组,初始化为0,开始处理结果不为even的情况。如果是up,那么就把左边天枰中的币加一,右边天枰中的币减一;如果是down则相反。最后遍历结果数组,找到绝对值最大的那个币,就是答案,如果大于0,就是重假币,否则是轻假币。

代码:

// @Team    : nupt2017team12// @Author  : Zst#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <string>#include <vector>#include <cmath>#include <algorithm>#include <map>using namespace std;#define LL long long#define MOD 1000000007#define CLR(a,x) memset(a,x,sizeof(a))#define INF 0x3f3f3f3f#define pb push_back#define FOR(i,a,b) for( int i = ( a ); i <= ( b ); ++i )struct node{    char A[7];    char B[7];    char result[5];    node()    {        CLR( A, 0 );        CLR( B, 0 );        CLR( result, 0 );    }}result[3];bool vis[12];int ans[12];vector<int> notEven;int main(){    // freopen( "1013.txt", "r", stdin );    int w;    scanf( "%d", &w );    while( w-- ) {        FOR( i, 0, 2 ) {            result[i] = node();        }        CLR( vis, false );        CLR( ans, 0 );        notEven.clear();        FOR( i, 0, 2 ) {            scanf( "%s%s%s", result[i].A, result[i].B, result[i].result );            if( strcmp( result[i].result, "even" ) == 0 ) {                int len = strlen( result[i].A );                FOR( j, 0, len-1 ) {                    char tmp = result[i].A[j];                    vis[tmp-'A'] = true;                }                FOR( j, 0, len-1 ) {                    char tmp = result[i].B[j];                    vis[tmp-'A'] = true;                }            } else {                notEven.pb( i );            }        }        FOR( i, 0, int(notEven.size())-1 ) {            int index = notEven[i];            string res = result[index].result;            int len = strlen( result[index].A );            FOR( j, 0, len-1 ) {                char tmp = result[index].A[j];                if( vis[tmp-'A'] )                    continue;                if( res == "up" )                    ans[tmp-'A']++;                else                    ans[tmp-'A']--;            }            FOR( j, 0, len-1 ) {                char tmp = result[index].B[j];                if( vis[tmp-'A'] )                    continue;                if( res == "up" )                    ans[tmp-'A']--;                else                    ans[tmp-'A']++;            }        }        int maxs = -1;        int maxid;        FOR( i, 0, 11 ) {            if( maxs < abs( ans[i] ) ) {                maxs = abs( ans[i] );                maxid = i;            }        }        if( ans[maxid] > 0 ) {            printf( "%c is the counterfeit coin and it is %s. \n", maxid + 'A', "heavy" );        } else {            printf( "%c is the counterfeit coin and it is %s. \n", maxid + 'A', "light" );        }    }    return 0;}

第二种

枚举24种情况,A为重假币,A为轻假币,B为……….

对于每一种情况,遍历天枰称量结果,判断枚举情况是否为真。

如果是重假币,那么就一定不会出现在结果为even的情况中。对于up结果,重假币一定是在左边天枰中,对于down结果,一定是在右边天枰中。

如果与判断矛盾,那么就否定这种情况。

最后找出与猜测不矛盾的情况,就是答案。

代码:

// @Team    : nupt2017team12// @Author  : Zst#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <string>#include <vector>#include <cmath>#include <algorithm>#include <map>using namespace std;#define LL long long#define MOD 1000000007#define CLR(a,x) memset(a,x,sizeof(a))#define INF 0x3f3f3f3f#define pb push_back#define FOR(i,a,b) for( int i = ( a ); i <= ( b ); ++i )struct node{    char A[7];    char B[7];    char result[5];    node()    {        CLR( A, 0 );        CLR( B, 0 );        CLR( result, 0 );    }}result[3];bool indexOf( char* str, char a ){    int len = strlen( str );    FOR( i, 0, len-1 ) {        if( str[i] == a )            return true;    }    return false;}int main(){    // freopen( "1013.txt", "r", stdin );    int w;    scanf( "%d", &w );    while( w-- ) {        FOR( i, 0, 2 ) {            result[i] = node();        }        FOR( i, 0, 2 ) {            scanf( "%s%s%s", result[i].A, result[i].B, result[i].result );        }        bool finds = false;        FOR( i, 0, 11 ) {            bool guessWrong = false;            // 怀疑是重假币            char tmp = i + 'A';            FOR( j, 0, 2 ) {                if( strcmp( result[j].result, "even" ) == 0 ) {                    // 因为是假币,所以不应该出现在even里面                    if( indexOf( result[j].A, tmp ) || indexOf( result[j].B, tmp ) ) {                        guessWrong = true;                        break;                    }                   } else if( strcmp( result[j].result, "up" ) == 0 ) {                    // 因为是重假币,所以一定在A里面                    if( !indexOf( result[j].A, tmp ) ) {                        guessWrong = true;                        break;                    }                    if( indexOf( result[j].B, tmp ) ) {                        guessWrong = true;                        break;                    }                } else if( strcmp( result[j].result, "down" ) == 0 ) {                    if( indexOf( result[j].A, tmp ) ) {                        guessWrong = true;                        break;                    }                    if( !indexOf( result[j].B, tmp ) ) {                        guessWrong = true;                        break;                    }                }            }            if( !guessWrong ) {                finds = true;                printf( "%c is the counterfeit coin and it is heavy. \n", tmp );                break;            }        }        if( !finds ) {            FOR( i, 0, 11 ) {                bool guessWrong = false;                // 怀疑是轻假币                char tmp = i + 'A';                FOR( j, 0, 2 ) {                    if( strcmp( result[j].result, "even" ) == 0 ) {                        // 因为是假币,所以不应该出现在even里面                        if( indexOf( result[j].A, tmp ) || indexOf( result[j].B, tmp ) ) {                            guessWrong = true;                            break;                        }                       } else if( strcmp( result[j].result, "up" ) == 0 ) {                        // 因为是轻假币,所以一定在B里面                        if( indexOf( result[j].A, tmp ) ) {                            guessWrong = true;                            break;                        }                        if( !indexOf( result[j].B, tmp ) ) {                            guessWrong = true;                            break;                        }                    } else if( strcmp( result[j].result, "down" ) == 0 ) {                        if( !indexOf( result[j].A, tmp ) ) {                            guessWrong = true;                            break;                        }                        if( indexOf( result[j].B, tmp ) ) {                            guessWrong = true;                            break;                        }                    }                }                if( !guessWrong ) {                    printf( "%c is the counterfeit coin and it is light. \n", tmp );                    break;                }            }        }    }    return 0;}
原创粉丝点击