UVa 10530 - Guessing Game

Contents

  1. 1. Problem
  2. 2. Solution
  3. 3. Code

Problem

題目網址
中文網址

Solution

一直紀錄上下界,不斷縮小範圍。最後看答案有沒有在內即可。

Code

UVa 10530
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <cstdio>

int main()
{
int up = 11, low = 0, n;
char str[20];
bool ok = true;

while (scanf("%d ", &n) && n)
{
fgets(str, 20, stdin);
if (str[4] == 'l' && n > low)
low = n;
else if (str[4] == 'h' && n < up)
up = n;
else if (str[0] == 'r')
{
if (n >= up || n <= low) //不在範圍內
ok = false;

puts(ok ? "Stan may be honest" : "Stan is dishonest");
ok = true;
up = 11;
low = 0;
continue;
}
}

return 0;
}