#UVa:10530-Guessing Game

在猜數字的過程中一直更新上界與下界,直到猜中時看看是否猜中的數字介在此範圍內即是答案。

C++(0.000)

/*******************************************************/
/* UVa 10530 Guessing Game                             */
/* Author: LanyiKnight [at] knightzone.org             */
/* Version: 2016/04/27                                 */
/*******************************************************/
#include <iostream>
#include <cstdio>
using namespace std;

int main(){
  int guessNumber;
  while( scanf("%d ", &guessNumber) != EOF && guessNumber != 0 ){
    int lowBound = 1, highBound = 10;
    string respond;
    while( getline(cin, respond) && respond != "right on" ){
      if( respond == "too high" ){
        highBound = min(highBound, guessNumber - 1 );
      }
      else if( respond == "too low" ){
        lowBound = max(lowBound, guessNumber + 1 );
      }

      scanf("%d ", &guessNumber);
    }

    if( guessNumber >= lowBound && guessNumber <= highBound ){
      printf("Stan may be honest\n");
    }
    else{
      printf("Stan is dishonest\n");
    }
  }
  return 0;
}

沒有迴響

本文還沒有迴響,快來搶頭香!

發表迴響

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料