#UVa:489-Hangman Judge

灆洢 2015-07-26 16:27:59

利用布林陣列紀錄要猜得字和猜過的字去進行比對,即可得解。

C++(0.625)

/*******************************************************/
/* UVa 489 Hangman Judge                               */
/* Author: Maplewing [at] knightzone.studio            */
/* Version: 2015/07/26                                 */
/*******************************************************/
#include <iostream>
#include <cstdio>
using namespace std;

bool isWin( bool allPossible[26], bool allUserGuess[26] ){
  bool isAllGuessed = true;
  for( int i = 0 ; i < 26 ; ++i ){
    if( allPossible[i] && !allUserGuess[i] ){
      isAllGuessed = false;
      break;
    }
  }

  return isAllGuessed;
}

int main(){
  int round;
  while( scanf("%d", &round) != EOF && round != -1 ){
    printf("Round %d\n", round);
    string answer, userAnswer;
    cin >> answer >> userAnswer;

    bool allPossible[26] = {0};
    for( int i = 0 ; i < answer.length() ; ++i ){
      allPossible[(answer[i]-'a')] = true;
    }

    int loseCount = 0;
    bool allUserGuess[26] = {0};
    for( int i = 0 ; i < userAnswer.length() ; ++i ){
      if( !allPossible[(userAnswer[i]-'a')] ){
        ++loseCount;
      }

      allUserGuess[(userAnswer[i]-'a')] = true;

      if( loseCount >= 7 ) break;
      if( isWin(allPossible, allUserGuess) ) break;
    }


    if( loseCount >= 7 ){
      printf("You lose.\n");
    }
    else if( isWin(allPossible, allUserGuess) ){
      printf("You win.\n");
    }
    else {
      printf("You chickened out.\n");
    }
  }

  return 0;
}

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

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