字符串回文与镜像

题意:回文———从前往后读与从后往前读字符串相同;镜像———字符串中字符镜像之后和原字符串互逆。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;
        else
            cout<<s<<" -- is not a palindrome."<<endl<<endl;
    }
    return 0;
}</span>


同样的思想,使用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");
		}
	}
}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值