当前位置:网站首页>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;
}
边栏推荐
- 接口测试如何准备测试数据
- Interface Test Framework Practice | Process Encapsulation and Test Case Design Based on Encrypted Interface
- BIOTIN ALKYNE CAS: 773888-45-2 Price, Supplier
- Power button 561. An array of split
- tag单调栈-单调栈预备知识-lt.739. 每日温度
- 2022/08/02 学习笔记 (day22) 多线程
- odps的临时查询能在写sql的时候就给结果一个命名不?
- 表的创建、修改与删除
- 【Biotin Azide|cas:908007-17-0】Price_Manufacturer
- 13.< tag-动态规划和回文字串>lt.647. 回文子串 + lt.516.最长回文子序列
猜你喜欢
Kotlin-Flow common encapsulation class: the use of StateFlow
typescript39-class类的可见修饰符
UV 裂解的生物素-PEG2-叠氮|CAS:1192802-98-4生物素接头
【Harmony OS】【ARK UI】Date 基本操作
High availability, two locations and three centers
三丁基-巯基膦烷「tBuBrettPhos Pd(allyl)」OTf),1798782-17-8
力扣561. 数组拆分
【 Harmony OS 】 【 ano UI 】 lightweight data storage
Interface Test Framework Practice (4) | Get Schema Assertion
IO进程线程->线程->day5
随机推荐
idea使用@Autowired注解爆红原因及解决方法
rosbag工具plotjuggler无法打开rosbag的问题
The problem that the rosbag tool plotjuggler cannot open rosbag
FileZilla 搭建ftp服务器
数据库基本概述与SQL概述
JS bottom handwriting
DFS's complement to pruning
【HMS core】【Ads Kit】Huawei Advertising——Overseas applications are tested in China. Official advertisements cannot be displayed
接口测试框架实战(一) | Requests 与接口请求构造
[Fine talk] Using native js to implement todolist
[Harmony OS] [ARK UI] ETS context basic operations
接口测试框架实战 | 流程封装与基于加密接口的测试用例设计
Bubble sort in c language structure
tag单调栈-单调栈预备知识-lt.739. 每日温度
Flink状态
typescript45-接口之间的兼容性
内部类、static关键字、final
业务表解析-余额系统
Detailed explanation of MOSN reverse channel
How to use the interface management tool YApi?Beautiful, easy to manage, super easy to use