zoukankan      html  css  js  c++  java
  • Palindromes

    要注意:单个字符是没有对应反序串的。

    几个测试:

     1 NOTAPALINDROME -- is not a palindrome.
    2
    3 ISAPALINILAPASI -- is a regular palindrome.
    4
    5 2A3MEAS -- is a mirrored string.
    6
    7 ATOYOTA -- is a mirrored palindrome.
    8
    9 B -- is a regular palindrome.
    10
    11 E -- is a regular palindrome.
    12
    13 8 -- is a mirrored palindrome.

    我用的 switch 结构:

    /* UVa 401 */
    # include <stdio.h>
    # include <string.h>

    # define MAXN 25

    char s[MAXN], ss[MAXN];

    char ch_map(char ch);
    void s_map(char *s, char *ss);
    int is_pal(char *s);

    int main()
    {
    int isPal, isMir, i;

    while (~scanf("%s", s))
    {
    for (i = 0; s[i] ; ++i)
    printf("%c", s[i]);
    s_map(s, ss);
    isPal = is_pal(s);
    isMir = is_pal(ss);
    if (strlen(s)==1 && ch_map(s[0])!=s[0]) isMir = 0;
    if (isPal && isMir) printf(" -- is a mirrored palindrome.\n");
    else if (isMir) printf(" -- is a mirrored string.\n");
    else if (isPal) printf(" -- is a regular palindrome.\n");
    else printf(" -- is not a palindrome.\n");
    printf("\n");
    }
    return 0;
    }

    char ch_map(char ch)
    {
    switch (ch)
    {
    case 'E': return '3';
    case 'J': return 'L';
    case 'L': return 'J';
    case 'S': return '2';
    case 'Z': return '5';
    case '2': return 'S';
    case '3': return 'E';
    case '5': return 'Z';
    case 'A': case 'H': case 'I':
    case 'M': case 'O': case 'T':
    case 'U': case 'V': case 'W':
    case 'X': case 'Y': case '1':
    case '8':return ch;
    default : return '0';
    }
    }

    void s_map(char *s, char *ss)
    {
    int i, len, L;
    len = strlen(s);
    L = len / 2;
    for (i = 0; i < L; ++i)
    ss[i] = s[i];
    for ( ; s[i] ; ++i)
    ss[i] = ch_map(s[i]);
    ss[i] = '\0';
    }

    int is_pal(char *s)
    {
    int i, len, L;
    len = strlen(s);
    L = len / 2;

    for (i = 0; i < L; ++i)
    if (s[i] != s[len-1-i]) return 0;
    return 1;
    }
  • 相关阅读:
    unittest框架assert断言
    python logging 总结
    好文章推荐 数据库mysql
    python 日志模块 日志格式
    django admin 后台总结(转载)
    xpath解析html标签
    lua 排序table.sort()用法
    lua 随机数 math.random()和math.randomseed()用法
    三种方法获取 lua时间戳
    crontab调用python脚本新思路
  • 原文地址:https://www.cnblogs.com/JMDWQ/p/2397525.html
Copyright © 2011-2022 走看看