字符串回文与镜像

题意:回文———从前往后读与从后往前读字符串相同;镜像———字符串中字符镜像之后和原字符串互逆。0(零)是回文,不是镜像字符,O(欧)既是回文又是镜像字符。

#include #include #include using namespace std; int main() { string s,a,b; char m[300]; memset(m,NULL,sizeof(m)); m['A']='A'; m['E']='3'; m['H']='H'; m['I']='I'; m['J']='L'; m['L']='J'; m['M']='M'; m['O']='O'; m['S']='2'; m['T']='T'; m['U']='U'; m['V']='V'; m['W']='W'; m['X']='X'; m['Y']='Y'; m['Z']='5'; m['1']='1'; m['2']='S'; m['3']='E'; m['5']='Z'; m['8']='8'; while(cin>>s){ a=b=""; int len=s.size(); for(int i=len-1; i>=0; i--) { a+=s[i]; // 判断是否回文 b+=m[s[i]]; // 判断是否镜像 } if (s==a && s==b) cout<



同样的思想,使用JAVA实现如下:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in) ; while(sc.hasNext()) { String s = sc.next() ; int len = s.length() ; char[] ch = s.toCharArray() ; char[] m = new char[300] ; m['A']='A'; m['E']='3'; m['H']='H'; m['I']='I'; m['J']='L'; m['L']='J'; m['M']='M'; m['O']='O'; m['S']='2'; m['T']='T'; m['U']='U'; m['V']='V'; m['W']='W'; m['X']='X'; m['Y']='Y'; m['Z']='5'; m['1']='1'; m['2']='S'; m['3']='E'; m['5']='Z'; m['8']='8'; String a , b ; a = b = "" ; for(int i = len - 1 ; i >= 0 ; i--) { a += ch[i] ; b += m[ch[i]] ; } if(s.compareTo(a) == 0 && s.compareTo(b) == 0) System.out.println(s + " -- is a mirrored palindrome.\n"); else if( s.compareTo(a) == 0 && s.compareTo(b) != 0) System.out.println(s + " -- is a regular palindrome.\n"); else if(s.compareTo(a) != 0 && s.compareTo(b) == 0) System.out.println(s + " -- is a mirrored string.\n"); else System.out.println(s + " -- is not a palindrome.\n"); } } }




【字符串回文与镜像】转载于:https://www.cnblogs.com/NYNU-ACM/p/4236813.html

    推荐阅读