这道题很水,我开始想的太复杂,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;
}