POJ 2328 Guessing Game(我的水题之路——猜数字测谎)

来源:互联网 发布:ps4数据导入pro主机 编辑:程序博客网 时间:2024/06/05 09:46
Guessing Game
Time Limit: 1000MS Memory Limit: 65536KTotal 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

10too high3too low4too high2right on5too low7too high6right on0

Sample Output

Stan is dishonestStan 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 onint 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;}


原创粉丝点击