算法入门经典P49-3-7(回文串和镜像串)

2021/11/3 11:10:17

本文主要是介绍算法入门经典P49-3-7(回文串和镜像串),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

#include<iostream>
#include<string.h>
#include<ctype.h> 
using namespace std;
const char* rev = "A   3  HIL JM O   2TUVWXY51SE Z  8 ";
const char*msg[] = {"not a palindrome","a regular parlindrome","a mirrored string","a mirrored palindrome"};
char r(char ch){
    if(isalpha(ch))return rev[ch-'A'];
    else return rev[ch-'0'+25];
}
int main(){
    char s[30];
    while(scanf("%s",s)==1){
        int len = strlen(s);
        int p = 1,m = 1;
        for(int i=0;i<(len+1)/2;i++){
            if(s[i]!=s[len-1-i])p = 0;
            if(r(s[i])!=s[len-1-i])m = 0;
        }
        printf("%s -- is %s.\n\n",s,msg[m*2+p]);
    }
    return 0;
}

 



这篇关于算法入门经典P49-3-7(回文串和镜像串)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程