zoukankan      html  css  js  c++  java
  • 回文词

    题意大概:

    输入一个只含大写字母和数字的字符串,判断这个字符串的性质:

    1只是镜像

    2只是回文

    3既是镜像又是回文

    4两个都不是


    回文:正着读反着读都一样

    镜像:

    A 的镜像A

    B           无

    C    

    D

    E   3

    F

    G

    H   H

    I   I

    J  L

    K  

    L   J

    M M

    N  

    O O

    P

    Q

    R

    S 2

    T T

    U U

    V V

    W W

    X X

    Y Y

    Z 5

    1 1

    2 S

    3 E

    4

    5 Z

    7

    8

    9

    源代码:

    #include <iostream>
    #include<string>
    using namespace std;
    char* a="A   3  HIL JM O   2TUVWXY51SE Z  8 ";
    string c[4]={"is not a palindrome","is a regular palindrome ","is a mirrored string","is a mirrored palindrome"};
    char r(char hh)
    {
        if(isalpha(hh))
        return a[hh-'A'];
        else
            return a[hh-'0'+25];
    }
    int main()
    {
        string b;
        cin>>b;
        int len=b.size();
        int x=1,y=1;
        for(int i=0;i<=((len+1)/2)-1;i++)
        {
            if(b[i]!=b[len-1-i])
                x=0;
            if(r(b[i])!=b[len-1-i])
                y=0;
        }
        if(x==0&&y==0)
            cout<<c[0]<<endl;
        else if(x==1&&y==0)
            cout<<c[1]<<endl;
        else if(x==0&&y==1)
            cout<<c[2]<<endl;
        else
            cout<<c[3]<<endl;
        return 0;
    }



    知识点:
    1

    char a;

    isalpha(a);    头文件:#include<ctype.h>

    用于判断a是否为英文字母,如果a是英文字母返回正整数,不是返回0



    2

    两个字符型字母(大写或小写),两个字符型整数相减,结果为数字。

    '5'-'1'=5

    'Z'-'A'=25

  • 相关阅读:
    49. Group Anagrams
    43. Multiply Strings
    22. Generate Parentheses
    17. Letter Combinations of a Phone Number
    8. String to Integer (atoi)
    【转】C#中base关键字的几种用法:base()
    【转】C#中virtual和abstract的区别
    [转]C#中的abstract 类和方法
    【转】C#虚方法virtual详解
    【转】C#中protected用法详解
  • 原文地址:https://www.cnblogs.com/iamjuruo/p/7470978.html
Copyright © 2011-2022 走看看