• UVa-401 Palindromes回文词


    虽然是水题,但是容易错。参照了紫书的代码可以写的很简洁。主要还是注意常量数组的使用,能让代码变得简单许多

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <string.h>
     4 #include <algorithm>
     5 #include <stack>
     6 #include <math.h>
     7 #pragma warning ( disable : 4996 )
     8 
     9 using namespace std;
    10 
    11 int Max( int a, int b ) { return a>b?a:b; }
    12 int Min( int a, int b ) { return a>b?b:a; }
    13 
    14 const int inf = 0x3f3f3f3f;
    15 const int maxn = 1000 + 10;
    16 const char T[100] = "A...3..HIL.JM.O...2TUVWXY51SE.Z..8.";
    17 char test[200];
    18 
    19 char getM( char c ) 
    20 {
    21     if (isalpha(c)) return T[c-'A'];
    22     else return T[c-'0'+25];
    23 }
    24 int main()
    25 {
    26     while ( ~scanf("%s", test) )
    27     {
    28         int len = strlen(test);
    29         bool isP = true, isM = true;
    30         for ( int i = 0; i < (len+1) / 2; i++ )
    31         {
    32             if ( test[i] != test[len-1-i] ) isP = false;
    33             if ( getM(test[i]) != test[len-1-i] ) isM = false;
    34         }
    35         if ((!isP)&&(!isM)) printf( "%s -- is %s
    
    ", test, "not a palindrome." );
    36         if (isP&&(!isM)) printf( "%s -- is %s
    
    ", test, "a regular palindrome." );
    37         if ((!isP)&&isM) printf( "%s -- is %s
    
    ", test, "a mirrored string." );
    38         if (isP&&isM) printf( "%s -- is %s
    
    ", test, "a mirrored palindrome." );
    39     }
    40     return 0;
    41 }
  • 相关阅读:
    类似吸顶功能解决ios不能实时监听onscroll的触发问题
    js 移动端识别手机号码
    H5输入框实时记录文字个数
    C语言指针和数组
    PHP变量
    PHP 的引用计数基础知识
    PHP提高效率的经验
    JS内置Function对象详解
    Javascript小细节总结
    浅析C++中内存分配的方式
  • 原文地址:https://www.cnblogs.com/chaoswr/p/8528949.html
Copyright © 2020-2023  润新知