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;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值