《算法竞赛入门经典2ndEdition 》例题3-3 回文词(Palindromes, Uva401)
来源:互联网 发布:ubuntu安装perl模块 编辑:程序博客网 时间:2024/05/22 12:31
好吧,这道题真心写了很长时间(不得不承认我很弱),现在还有一个程序没改出来,贴出来希望各位大神能给我挑挑错,下面这个是没有AC的代码:
#include <cstdio>#include <cstring>#include "string.h"char s[30];using namespace std;bool check_palindrome();bool check_mirrored(int, int);int main(){ //freopen("New Text Document.txt","r",stdin); //freopen("Output.txt","w",stdout); while(scanf("%s", s) != EOF) { int n = strlen(s); printf("%s ", s); if(check_palindrome())// a palindrome { int flag = 1; for(int i = 0; i <= n/2; i++)//= if(!check_mirrored(i,n-i-1)) { flag = 0; printf("-- is a regular palindrome.\n"); break; } if(flag == 1) printf("-- is a mirrored palindrome.\n"); } else//not a palindrome { int flag = 1; for(int i = 0; i <= n/2; i++)//= if(!check_mirrored(i,n-i-1)) { flag = 0; printf("-- is not a palindrome.\n"); break; } if(flag == 1) printf("-- is a mirrored string.\n"); } printf("\n"); } //getchar();getchar(); return 0;} bool check_palindrome(){ int n = strlen(s); for(int i = 0; i < n/2; i++) if(s[i] != s[n-i-1]) return false; return true;}bool check_mirrored(int x,int y){ char a = s[x], b = s[y]; if(a == b) switch(a) { 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 true; //default:return false; } else { if(a > b) { char ch; ch = a; a = b; b = ch; } switch(a) { case '2': if(b == 'S') return true; else return false; case '3': if(b == 'E') return true; else return false; case '5': if(b == 'Z') return true; else return false; case 'J': if(b == 'L') return true; else return false; //default: return false; } return false; }}
下面这个是看了书之后使用常量数组简化了一下check_mirrored函数,就AC了,但目前还没找到上个程序的错误。
#include <cstdio>#include <cstring>char s[30];const char* rev = "A 3 HIL JM O 2TUVWXY51SE Z 8 ";bool check_palindrome();bool check_mirrored();int main(){ //freopen("New Text Document.txt","r",stdin); //freopen("Output.txt","w",stdout); while(scanf("%s", s) != EOF) { printf("%s ", s); if(check_palindrome())// a palindrome if(!check_mirrored()) printf("-- is a regular palindrome.\n"); else printf("-- is a mirrored palindrome.\n"); else//not a palindrome if(!check_mirrored()) printf("-- is not a palindrome.\n"); else printf("-- is a mirrored string.\n"); printf("\n"); } //getchar();getchar(); return 0;} bool check_palindrome(){ int n = strlen(s); for(int i = 0; i <= n/2; i++) if(s[i] != s[n-i-1]) return false; return true;}bool check_mirrored(){ int n = strlen(s); for(int i = 0; i <= n/2; i++) if(s[i]<='Z' && s[i]>='A' && *(rev+s[i]-'A')==s[n-i-1]) continue; else if(s[i]<='9' && s[i]>='1' && *(rev+s[i]-'1'+26)==s[n-i-1]) continue; else return false; return true;}
虽然AC了,但还是觉得没有Rujia Liu给出的代码简洁,下面这个是从书配套的代码仓库下载的代码。
https://github.com/aoapc-book/aoapc-bac2nd/blob/master/ch3/UVa401.cpp
// UVa401 Palindromes// Rujia Liu#include<stdio.h>#include<string.h>#include<ctype.h>const char* rev = "A 3 HIL JM O 2TUVWXY51SE Z 8 ";const char* msg[] = {"not a palindrome", "a regular palindrome", "a mirrored string", "a mirrored palindrome"};char r(char ch) { if(isalpha(ch)) return rev[ch - 'A']; 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;}
1 0
- 《算法竞赛入门经典2ndEdition 》例题3-3 回文词(Palindromes, Uva401)
- 《算法竞赛入门经典2ndEdition 》例题3-2 WERTYU(WERTYU, Uva10082)
- 《算法竞赛入门经典2ndEdition 》例题3-1 TeX中的引号(Tex Quotes, Uva 272)
- 《算法竞赛入门经典2ndEdition 》例题3-5 生成元(Digit Generator, Uva1583)
- 《算法竞赛入门经典2ndEdition 》例题3-6 环状序列(Circular Sequence, Uva1584)
- 算法竞赛入门经典 回文词 UVa401 msg数组
- 《算法竞赛入门经典2ndEdition》 例题3-4 猜数字游戏的提示(Master-Mind Hints, Uva340)
- 《算法竞赛入门经典2ndEdition 》例题5-3 安迪的第一个字典(Andy's First Dictionary, Uva10815)
- 算法竞赛入门经典 例题 3-4 回文串
- 《算法竞赛入门经典2ndEdition 》例题5-2 木块问题(The Blocks Problem, Uva101)
- 《算法竞赛入门经典2ndEdition 》例题5-1 大理石在哪儿(Where is the marble?, Uva10474)
- 《算法竞赛入门经典2ndEdition 》例题5-4 反片语(Ananagrams, Uva156)
- 《算法竞赛入门经典2ndEdition 》例题5-5 集合栈计算机(The SetStack Computer, Uva12096)
- 《算法竞赛入门经典2ndEdition 》例题5-6 团体队列(Team Queue, Uva540)
- 《算法竞赛入门经典2ndEdition 》例题5-7 丑数(Ugly Numbers, Uva136)
- 《算法竞赛入门经典2ndEdition 》例题5-8 Unix is 命令(Unix is, Uva400)
- 《算法竞赛入门经典2ndEdition 》例题5-9 数据库(Database, Uva1592)
- 算法之路二:刘汝佳算法竞赛入门经典 3.7回文词 UVa401
- k近邻法
- java 简化文件夹的复制;
- css:appearance:none;
- 初学Python-第二章练习题
- Java多线程中join方法的理解
- 《算法竞赛入门经典2ndEdition 》例题3-3 回文词(Palindromes, Uva401)
- 【C#/算法】RGB、HSV、HSL颜色模型的互相转换
- Android第三方开源水面波浪波形view:WaveView(电量、能量、容量指示)
- 网站开发进阶(二十)JS中window.alert()与alert()的区别
- Java中获取键盘输入值的三种方法
- 机器学习——边角料
- STM32之SysTick学习
- 内存对齐规则
- 黑马程序员——集合知识概括(上)