PAT (Basic Level) Practice (中文)1079 延迟的回文数(C语言)
程序员入门水平,贴出代码大家一起进步!
题目
给定一个 k+1 位的正整数 N,写成 ak ⋯a1a0的形式,其中对所有 i 有 0≤a<10且 ak>0。N 被称为一个回文数,当且仅当对所有 i 有 ai=ak−i。零也被定义为一个回文数。
非回文数也可以通过一系列操作变出回文数。首先将该数字逆转,再将逆转数与该数相加,如果和还不是一个回文数,就重复这个逆转再相加的操作,直到一个回文数出现。如果一个非回文数可以变出回文数,就称这个数为延迟的回文数。(定义翻译自 https://en.wikipedia.org/wiki/Palindromic_number )
给定任意一个正整数,本题要求你找到其变出的那个回文数。
输入格式:
输入在一行中给出一个不超过1000位的正整数。
输出格式:
对给定的整数,一行一行输出其变出回文数的过程。每行格式如下
A + B = C
其中 A 是原始的数字,B 是 A 的逆转数,C 是它们的和。A 从输入的整数开始。重复操作直到 C 在 10 步以内变成回文数,这时在一行中输出 C is a palindromic number.;或者如果 10 步都没能得到回文数,最后就在一行中输出 Not found in 10 iterations.。
输入样例 1:
97152
输出样例 1:
97152 + 25179 = 122331
122331 + 133221 = 255552
255552 is a palindromic number.
输入样例 2:
196
输出样例 2:
196 + 691 = 887
887 + 788 = 1675
1675 + 5761 = 7436
7436 + 6347 = 13783
13783 + 38731 = 52514
52514 + 41525 = 94039
94039 + 93049 = 187088
187088 + 880781 = 1067869
1067869 + 9687601 = 10755470
10755470 + 07455701 = 18211171
Not found in 10 iterations.
思路||总结
- 最多有1000位,用字符数组模拟加法。
- 设置三个函数(反转函数、回文判别函数、单位加法函数)。
AC代码
#include <stdio.h> #include <stdlib.h> #include <string.h> #define NUM 1100 int main() { int clip(char str[]); int add(char str1[],char str2[],char str3[]); char str1[NUM],str2[NUM],str3[NUM]; scanf("%s",str1); int i=10,flag=clip(str1);//clip返回0则直接输出。 while(i>0&&flag==1) { flag=add(str1,str2,str3); i--; }; if(flag==0)printf("%s is a palindromic number.",str1); else printf("Not found in 10 iterations."); return 0; } int add(char str1[],char str2[],char str3[]) { int clip(char str[]); void reverse(char str[]); int i,l=strlen(str1); for(i=0;i<l;i++)str2[i]=str1[l-i-1]; str2[l]='\0'; int flag=0;//进位表示默认为0。 for(i=0;i<l;i++) { str3[i]=(str1[i]-'0'+str2[i]-'0'+flag)%10+'0';//字符型转化为整型进行相加再将相加的结果转化为字符型。 flag=(str1[i]+str2[i]+flag-str3[i]-'0')/10; }; if(flag>0)str3[l++]=flag+'0';//最后一位有进位则加一。 str3[l]='\0'; reverse(str3);//字符串翻转为相加的结果。 printf("%s + %s = %s\n",str1,str2,str3); strcpy(str1,str3); return clip(str1); } void reverse(char str[])//字符串翻转函数。 { int i,l=strlen(str); char tem[NUM]; strcpy(tem,str); for(i=0;i<l;i++)str[i]=tem[l-i-1]; } int clip(char str[])//回形数判断函数,返回0为回文数。 { int flag=0; int i,l=strlen(str); for(i=0;i<=(int)((l-1)/2);i++)//两头向中间比较,比较到中间即可。 if(str[i]!=str[l-i-1]) { flag=1; break; }; return flag; }
- PAT (Basic Level) Practise (中文) 1079. 延迟的回文数 (20)
- PAT (Basic Level) Practice (中文)1088 三人行(C语言)
- PAT (Basic Level) Practice (中文)1080 MOOC期终成绩(C语言)
- PAT (Basic Level) Practice (中文)1089 狼人杀-简单版(C语言)
- PAT (Basic Level) Practise (中文) 1079. 延迟的回文数 (20)
- PAT (Basic Level) Practice (中文)1081 检查密码(C语言)
- PAT (Basic Level) Practice (中文)1085 PAT单位排行(C语言)
- PAT (Basic Level) Practice (中文)1086 就不告诉你(C语言)
- PAT (Basic Level) Practice (中文)1082 射击比赛(C语言)
- PAT (Basic Level) Practice (中文)1087 有多少不同的值(C语言)
- PAT (Basic Level) Practice (中文)1009 说反话
- PAT (Basic Level) Practise (中文)1002. 写出这个数 (20) C语言
- PAT (Basic Level) Practise (中文)1012. 数字分类 (20) C语言
- PAT (Basic Level) Practise (中文)1025. 反转链表 (25)C语言
- PAT (Basic Level) Practise (中文)1044. 火星数字(20) C语言
- PAT (Basic Level) Practice (中文)-1004
- PAT (Basic Level) Practise (中文)1003. 我要通过!(20) C语言
- PAT (Basic Level) Practise (中文)1013. 数素数 (20) C语言
- PAT (Basic Level) Practise (中文)1017. A除以B (20) C语言
- PAT (Basic Level) Practise (中文)1039. 到底买不买(20) C语言