poj 2328 Guessing Game

简介:

这道题很水,我开始想的太复杂,WA了很多次。。。

不过最后还是被我磨AC了。。。


AC的代码(简单版):

#include <stdio.h>
#include <string.h>

char words[10]; //stan's words
int low;  //记录中间可行的数
int high;

void init()
{
	low=0;
	high=11;
}

int main()
{
	int num;
	int i;
	char tmp[10];

	init();

	while(scanf("%d",&num) && num!=0)
	{
		scanf("%s",tmp);
		scanf("%s",words);

		if(strcmp(words,"high")==0)
		{
			if(num<high)
				high=num;
		}

		else if(strcmp(words,"low")==0)
		{
			if(num>low)
				low=num;
		}

		else if(strcmp(words,"on")==0)
		{
			if(num<=low || num>=high)
				printf("Stan is dishonest\n");

			else
				printf("Stan may be honest\n");

			//重新初始化下一轮游戏
			init();
		}
	}

	return 0;
}



AC的代码(复杂版):

#include <stdio.h>
#include <string.h>

char words[10]; //stan's words
int flag[11];//为1代表已排除
bool result;
int low;  //记录中间可行的数
int high;

void init()
{
	memset(flag,0,sizeof(flag));
	result=false; //已得出撒谎的结果则为true
	low=0;
	high=11;
}

int main()
{
	int num;
	int i;
	char tmp[10];

	init();

	while(scanf("%d",&num) && num!=0)
	{
		scanf("%s",tmp);
		scanf("%s",words);

		if(result==false && strcmp(words,"high")==0)
		{
			//就是说这个之上的数不可能了
			for(i=num;i<=10;i++)
			{
				//说他too high,发现之前说他太低
				if(flag[i]==1)
					result=true;
				flag[i]=2;
			}

			if(num<high)
				high=num;
		}

		else if(result==false && strcmp(words,"low")==0)
		{
			//之下的数不可能了
			for(i=1;i<=num;i++)
			{
				if(flag[i]==2)
					result=true;
				flag[i]=1;
			}

			if(num>low)
				low=num;
		}

		else if(strcmp(words,"on")==0)
		{
			if(result==true)
				printf("Stan is dishonest\n");

			else if(num<=low || num>=high)
				printf("Stan is dishonest\n");

			else
				printf("Stan may be honest\n");

			//重新初始化下一轮游戏
			init();
		}
	}

	return 0;
}





相关文章
|
7月前
|
算法
uva 10891 game of sum
题目链接 详细请参考刘汝佳《算法竞赛入门经典训练指南》 p67
15 0
LeetCode 390. Elimination Game
给定一个从1 到 n 排序的整数列表。 首先,从左到右,从第一个数字开始,每隔一个数字进行删除,直到列表的末尾。 第二步,在剩下的数字中,从右到左,从倒数第一个数字开始,每隔一个数字进行删除,直到列表开头。 我们不断重复这两步,从左到右和从右到左交替进行,直到只剩下一个数字。 返回长度为 n 的列表中,最后剩下的数字。
74 0
LeetCode 390. Elimination Game
codeforces327——A. Flipping Game(前缀和)
codeforces327——A. Flipping Game(前缀和)
66 0
|
人工智能
Codeforces 839B Game of the Rows【贪心】
B. Game of the Rows time limit per test:1 second memory limit per test:256 megabytes input:standard input output:standard output...
1138 0
|
Java
HDU 5882 Balanced Game
Balanced Game Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 508    Accepted Submission(s): ...
817 0