UVA - 401 - Palindromes

来源:互联网 发布:保罗10年季后赛数据 编辑:程序博客网 时间:2024/06/11 18:32

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=96&page=show_problem&problem=342


题意:

  辨别输入的字符串是否对称或自身镜像。


解题:

  本是水题,可给行输入搞晕了。检查了半天。

  cin.getline WA,但gets就能过,但gets是系统不推荐使用的。

  解题用gets,其实用cin.getline。


#include <iostream>#include <cstring>#include <stdio.h>using namespace std;const int MAXLENGTH = 20;int main(){char LETTER_REVERSE[26] = {'A', ' ', ' ', ' ', '3', ' ', ' ','H', 'I', 'L', ' ', 'J', 'M', ' ','O', ' ', ' ', ' ', '2', 'T', 'U', 'V', 'W', 'X', 'Y', '5'};char NUMBER_REVERSE[10] ={' ', '1', 'S', 'E', ' ', 'Z', ' ', ' ', '8', ' '};// WA//char str[100];//while ( fgets(str, 100, stdin) != NULL )// WA// char str[100];// while ( cin.getline(str, MAXLENGTH) )// WA// string str;// while ( getline(cin, str) )        char str[100]; while ( gets(str) ){bool bMirror = true;bool bPalindrome = true;// for ( int i=0, j=str.length()-1; i<=j; i++, j-- )for ( int i=0, j=strlen(str)-1; i<=j; i++, j-- ){if ( str[i] != str[j] ){bPalindrome = false;} // end ifif ( isalpha(str[i]) ){if ( LETTER_REVERSE[str[i]-'A'] != str[j] ){bMirror = false;} // end if} // end ifelse{if ( NUMBER_REVERSE[str[i]-'0'] != str[j] ){bMirror = false;} // end if} // end else} // end forcout <<str;if ( bMirror && bPalindrome ){cout <<" -- is a mirrored palindrome." <<'\n';} // end ifelse{if ( bMirror && !bPalindrome ){cout <<" -- is a mirrored string." <<'\n';} // end ifelse{if ( !bMirror && bPalindrome ){cout <<" -- is a regular palindrome." <<'\n';} // end ifelse{cout <<" -- is not a palindrome." <<'\n';} // end else} // end else} // end elsecout <<'\n';} // end whilereturn 0;}


0 0
原创粉丝点击