字符串回文与镜像
題意:回文———從前往后讀與從后往前讀字符串相同;鏡像———字符串中字符鏡像之后和原字符串互逆。0(零)是回文,不是鏡像字符,O(歐)既是回文又是鏡像字符。
<span style="font-size:18px;">#include <iostream> #include <string.h> #include <string>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<<s<<" -- is a mirrored palindrome."<<endl<<endl;else if (s==a && s!=b)cout<<s<<" -- is a regular palindrome."<<endl<<endl;else if (s!=a && s==b)cout<<s<<" -- is a mirrored string."<<endl<<endl;elsecout<<s<<" -- is not a palindrome."<<endl<<endl;}return 0; }</span>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");elseSystem.out.println(s + " -- is not a palindrome.\n");}} }
轉載于:https://www.cnblogs.com/NYNU-ACM/p/4236813.html
總結
- 上一篇: 用u盘装win10正版系统怎么安装 u盘
- 下一篇: 怎么解除认证过的加密的u盘 解除加密的U