• 字符串回文与镜像


    题意:回文———从前往后读与从后往前读字符串相同;镜像———字符串中字符镜像之后和原字符串互逆。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.
    ");
    			else if( s.compareTo(a) == 0 && s.compareTo(b) != 0)
    				System.out.println(s + " -- is a regular palindrome.
    ");
    			else if(s.compareTo(a) != 0 && s.compareTo(b) == 0)
    				System.out.println(s + " -- is a mirrored string.
    ");
    			else
    				System.out.println(s + " -- is not a palindrome.
    ");
    		}
    	}
    }



  • 相关阅读:
    2021软件工程-个人阅读作业
    OO第四单元——基于UML的UML解析器总结&OO课程总结
    OO第三单元——基于JML的社交网络总结
    OO第二单元——电梯作业总结
    SQL拼接字符串
    SQL查询列表中每种类型的第一条
    JS获取当前时间,设置不可用以前的时间
    JavaScript中的函数使用
    .Net软件开发面试技巧
    .Net小白的第一篇博客
  • 原文地址:https://www.cnblogs.com/NYNU-ACM/p/4236813.html
Copyright © 2020-2023  润新知