题目链接;https://vjudge.net/problem/POJ-2328
题目
两个小盆友玩猜数字游戏,一个小盆友心里想着1~10中的一个数字,另一个小盆友猜。如果猜的数字比实际的大,则告诉他“too high”,小则“too low”,正好则“right on”。直到猜对为止。但是那个猜的朋友怀疑他的小伙伴作弊,给他的回答不正确。于是让你根据他们的对话来判断一下这个小伙伴是否说谎。
Sample Input
10
too high
3
too low
4
too high
2
right on
5
too low
7
too high
6
right on
0
Sample Output
Stan is dishonest
Stan may be honest
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
char ch[20];
int n;
int l=0,r=11;
while(scanf("%d",&n)!=EOF&&n)
{
while(gets(ch)&&strcmp(ch,"right on"))
{
if(strcmp(ch,"too high")==0)
{
if(n<r)
r=n;
}
if(strcmp(ch,"too low")==0)
{
if(n>l)
l=n;
}
scanf("%d",&n);
}
if(n>l&&n<r)
{
cout<<"Stan may be honest"<<endl;
}
else
{
cout<<"Stan is dishonest"<<endl;
}
l=0,r=11;
}
return 0;
}
记得改成C语言交
代码
#include"stdio.h"
#include"string.h"
int main()
{
int n,x,y,i,j,k;
char s[20];
int a[11];
while(scanf("%d",&n)!=EOF&&n)
{
memset(a,0,sizeof(a));
while(gets(s)&&strcmp(s,"right on"))
{
if(!strcmp(s,"too high"))
{
for(i=n;i<=10;i++)
a[i]=1;
}
if(!strcmp(s,"too low"))
{
for(i=n;i>=1;i--)
a[i]=1;
}
scanf("%d",&n);
}
if(a[n]==0)
printf("Stan may be honest\n");
else
printf("Stan is dishonest\n");
}
return 0;
}