UVa-401-Palindromes

//2016年/6/3日重刷
#include <map>
#include <cstdio>
#include <string>
#include <cstring>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;

const string s ="ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
const string s1="A   3  HIL JM O   2TUVWXY51SE Z  8 ";

int main()
{
    char c;
    string s2;
    //freopen("in1.txt","r",stdin);
    map<char,char>M;
    for(int i=0;i<s.size();i++)
        M[s[i]]=s1[i];
    while(cin>>s2)
    {
        string s3=s2;
        reverse(s3.begin(),s3.end());
        if(s3==s2){
            int flag=1;
            for(int i=0;i<s2.size();i++)
                if(M[s3[i]]!=s2[i]){
                    flag=0;
                    break;
                }
            if(flag)
                cout<<s2<<" -- is a mirrored palindrome."<<endl<<endl;
            else
                cout<<s2<<" -- is a regular palindrome."<<endl<<endl;
        }
        else{
            int flag=1;
            for(int i=0;i<s2.size();i++)
                if(M[s3[i]]!=s2[i]){
                    flag=0;
                    break;
                }
            if(flag)
                cout<<s2<<" -- is a mirrored string."<<endl<<endl;
            else
                cout<<s2<<" -- is not a palindrome."<<endl<<endl;
        }
    }
    return 0;
}
//下面是书中代码
#include<cstdio>
#include<ctype.h>
#include<cstring>
#include<fstream>
#include<iostream>
//#define LOCAL
using namespace std;
const char* s1[]={"not a palindrome.","a regular palindrome.","a mirrored string.","a mirrored palindrome."};
const char* s2="A   3  HIL JM O   2TUVWXY51SE Z  8 ";
char r(char c)
{
        if(isalpha(c)) return s2[c-'A'];
        else return s2[c-'0'+25];
}
int main()
{
#ifdef LOCAL
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
#endif // LOCAL
    char s[25];
    while(scanf("%s",s)==1)
    {
        int m=1,n=1;
        int len=strlen(s);
        for(int i=0; i<(len+1)/2; i++)
        {
            if(s[i]!=s[len-i-1]) m=0;
            if(r(s[i])!=s[len-i-1]) n=0;
        }
        printf("%s -- is %s\n\n",s,s1[2*n+m]);
    }
    return 0;
}

总结

①声明一个二维字符数组 char* s1[]={ "","","" };

    声明一个一维字符数组 char* s2=“”;

②isalpha 在ctype.h中定义

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值