UVA10530 - Guessing Game

UVA10530 - Guessing Game

四月 06, 2019

題目簡述

你和 Stan 在玩終極炸彈,如果你猜太高 Stan 會說 “too high”,猜太低會說 “too low”,直到你猜中則 Stan 會說 “right on”,並結束這回合的遊戲。
要判斷 Stan 在遊戲過程中有沒有說謊

想法

維護猜的數字範圍的上界和下界,並依照每次得到的新訊息更新上下界。
則 Stan 是誠實的若且唯若 Stan 說 “right on” 的數字在上下界中且遊戲過程中上界沒有低於過下界

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
* UVA10530 - Guessing Game
* Author: ifTNT
*/
#include <iostream>
#include <string>
using namespace std;

int main(){
int guess;
int high=10, low=1;
bool honest=true;
string result;

while(true){
cin >> guess;
if(guess==0) break;
cin.ignore();
getline(cin, result);

if(result=="too high" && guess-1<high){
high = guess-1;
}
if(result=="too low" && guess+1>low){
low = guess+1;
}
if(high<low){
honest = false;
}
if(result=="right on"){
honest &= (guess<=high && guess>=low);
if(honest){
cout << "Stan may be honest" << endl;
}else{
cout << "Stan is dishonest" << endl;
}

high=10;
low=1;
honest=true;
}
}
return 0;
}