#UVa:10945-Mother bear

灆洢 2015-12-02 14:07:05

將輸入的字串忽略掉非英文字母的字,並統一大小寫,最後做迴文判斷即可得解。

C++(0.003)

/*******************************************************/
/* UVa 10945 Mother bear                               */
/* Author: Maplewing [at] knightzone.studio            */
/* Version: 2015/12/02                                 */
/*******************************************************/
#include <iostream>
#include <cstdio>
#include <cctype>
#include <string>
using namespace std;

bool isPalindrome(string s){
  if( s.length() == 0 ){
    return true;
  }

  for( int i = 0 ; i <= s.length() / 2 ; ++i ){
    if( s[i] != s[s.length()-i-1] ){
      return false;
    }
  }
  return true;
}

int main(){
  string input;
  while( getline(cin, input) && input != "DONE" ){
    string line;
    for( int i = 0 ; i < input.length() ; ++i ){
      if( isalpha(input[i]) ){
        line += tolower(input[i]);
      }
    }

    if( isPalindrome(line) ){
      printf("You won't be eaten!\n");
    }
    else{
      printf("Uh oh..\n");
    }

  }

  return 0;
}

發表迴響

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