44
技術社區[雲棲]
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;
}
最後更新:2017-04-03 05:39:54