当前位置:网站首页>1079 延迟的回文数 (20 分)
1079 延迟的回文数 (20 分)
2022-08-03 05:09:00 【破烂摆烂人】
给定一个 k+1 位的正整数 N,写成 a
k
⋯a
1
a
0
的形式,其中对所有 i 有 0≤a
i
<10 且 a
k
>0。N 被称为一个回文数,当且仅当对所有 i 有 a
i
=a
k−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.
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
char a[1010] , b[1010] = {
0} ;
scanf("%s",&a) ;
int i , j ;
for( i = 0 ; i < 10 ; i++ ){
//变成回文的步数
int k = strlen(a) - 1 , flag = 1 ;
for( j = 0 ; j < (k+1)/2 ; j++ ){
//判断是否为回文
if( a[j] != a[k-j] ){
flag = 0 ;
break ;
}
}
if( flag == 1 ){
printf("%s is a palindromic number.",a) ;
break ;
}else{
for( j = 0 ; j < k + 1 ; j++ ){
b[j] = a[k-j] ;
}
printf("%s + %s = ", a, b) ;
int sum , part = 0;
for( j = k ; j >= 0 ; j-- ){
sum = a[j] - '0' + b[j] - '0' + part;
a[j] = sum % 10 + '0';
part = sum / 10;
}
if(part){
memmove(a + 1, a, k + 2);
a[0] = part + '0';
}
printf("%s\n", a);
}
}
if( i == 10 ){
printf("Not found in 10 iterations.") ;
}
return 0;
}
边栏推荐
猜你喜欢

shell脚本循环语句

Common lipophilic cell membrane dyes DiO, Dil, DiR, Did spectrograms and experimental procedures

Interface Test Framework Practice (4) | Get Schema Assertion

传统企业如何转型社交电商,泰山众筹的玩法有哪些?

WinForm的控件二次开发

typescript44-对象之间的类兼容器

修饰生物素DIAZO-生物素-PEG3-DBCO|重氮-生物素-三聚乙二醇-二苯基环辛炔

Alienware上线首个数字时装AR试穿体验

How to prepare for the test interface test data

Kotlin-Flow常用封装类:StateFlow的使用
随机推荐
[Developers must see] [push kit] Collection of typical problems of push service service 2
软件开发的最大的区别是什么?
C#异步和多线程
Interface test Mock combat (2) | Combined with jq to complete batch manual Mock
自组织是管理者和成员的双向奔赴
Super handy drawing tool is recommended
Modified BiotinDIAZO-Biotin-PEG3-DBCO|diazo-biotin-tripolyethylene glycol-diphenylcyclooctyne
Kotlin-Flow common encapsulation class: the use of StateFlow
FileZilla 搭建ftp服务器
Bubble sort in c language structure
GIS数据漫谈(五)— 地理坐标系统
【Harmony OS】【ARK UI】ets use startAbility or startAbilityForResult to invoke Ability
c语言结构体中的冒泡排序
DFS对剪枝的补充
【Harmony OS】【ARK UI】ets使用startAbility或startAbilityForResult方式调起Ability
Harmony OS Date ano UI 】 【 】 the basic operation
Practical application of WebSocket
接口测试框架实战(二)| 接口请求断言
typescript42-readonly修饰符
js实现一个 bind 函数