天天看點

POJ 2328 Guessing Game(我的水題之路——猜數字測謊)

Guessing Game

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12345 Accepted: 4333

Description

Stan and Ollie are playing a guessing game. Stan thinks of a number between 1 and 10 and Ollie guesses what the number might be. After each guess, Stan indicates whether Ollie's guess is too high, too low, or right on. 

After playing several rounds, Ollie has become suspicious that Stan cheats; that is, that he changes the number between Ollie's guesses. To prepare his case against Stan, Ollie has recorded a transcript of several games. You are to determine whether or not each transcript proves that Stan is cheating.

Input

Standard input consists of several transcripts. Each transcript consists of a number of paired guesses and responses. A guess is a line containing single integer between 1 and 10, and a response is a line containing "too high", "too low", or "right on". Each game ends with "right on". A line containing 0 follows the last transcript.

Output

For each game, output a line "Stan is dishonest" if Stan's responses are inconsistent with the final guess and response. Otherwise, print "Stan may be honest".

Sample Input

10
too high
3
too low
4
too high
2
right on
5
too low
7
too high
6
right on
0
      

Sample Output

Stan is dishonest
Stan may be honest
      

Source

Waterloo local 2003.07.05

猜數字遊戲比較熟悉,是說先确定一個正确數字,另一個人猜數字,然後回答猜測數字比正确數字是太高、太低還是正确。現在題中給定猜測的過程,讓我們用程式驗證這個猜測過程中,回答是否正确。

一拿到這道題,先想的是否能夠邊讀入邊判斷,因為這樣會很簡單,但是發現不行,因為正确的數字隻有在最後才能得到,是以用兩個int數組getnum、answer分别記錄猜測的數字和猜測結果,其中answer數組用-1/1/0分别表是“too low”、“too high”、“right on”.直到讀取到正确的數字,将它儲存,然後從頭驗證getnum中第i個元素的比較結果和answer中第i條記錄是否相符,如果全部相符則說明全部正确,輸出“Stan may be honest”,發現有不符合的立刻退出循環,輸出“Stan is dishonest”.

代碼(1AC):

#include <cstdio>
#include <cstdio>
#include <cstring>

char str[3][20]={"too high", "too low", "right on"};
int answer[1100]; // -1 too low, 1 too high, 0 right on
int getnum[1100];

int getanswer(char ans[]){
    if (strcmp(str[0], ans) == 0){
        return 1;
    }
    else if (strcmp(str[1], ans) == 0){
        return -1;
    }
    else if (strcmp(str[2], ans) == 0){
        return 0;
    }
}

int main(void){
    char tmp[20];
    int num, rightnum;
    int i, j;
    int flag; // 0 dishonst 1 honst

    while (scanf("%d", &num), num != 0){
        getchar();
        gets(tmp);
        answer[0] = getanswer(tmp);
        getnum[0] = num;
        for (i = 0; answer[i] != 0; ){
            i++;
            scanf("%d", &num);
            getchar();
            gets(tmp);
            answer[i] = getanswer(tmp);
            getnum[i] = num;
        }
        rightnum = getnum[i];
        i++; // i means the number of the responses
        for (flag = 1, j = 0; j < i - 1; j++){
            if (getnum[j] - rightnum > 0 && answer[j] == 1 ){
                continue;
            }
            else if (getnum[j] - rightnum < 0 && answer[j] == -1){
                continue;
            }
            else{
                flag = 0;
                break;
            }
        }
        if (flag){
            printf("Stan may be honest\n");
        }
        else if (flag == 0){
            printf("Stan is dishonest\n");
        }
    }
    return 0;
}
           

繼續閱讀