当前位置:网站首页>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;
}
边栏推荐
- 【软件工程之美 - 专栏笔记】35 | 版本发布:软件上线只是新的开始
- 【Harmony OS】【ARK UI】轻量级数据存储
- MOSN 反向通道详解
- Unity2D horizontal board game tutorial 6 - enemy AI and attack animation
- 接口测试框架实战(四)| 搞定 Schema 断言
- 业务表解析-余额系统
- 接口和抽象
- Kotlin-Flow common encapsulation class: the use of StateFlow
- closures in js
- 【Harmony OS】【FAQ】Hongmeng Questions Collection 1
猜你喜欢
Tributyl-mercaptophosphane "tBuBrettPhos Pd(allyl)" OTf), 1798782-17-8
Harmony OS Date ano UI 】 【 】 the basic operation
【生物素叠氮化物|cas:908007-17-0】价格_厂家
多肽介导PEG磷脂——靶向功能材料之DSPE-PEG-RGD/TAT/NGR/APRPG
Kotlin-Flow common encapsulation class: the use of StateFlow
Exception(异常) 和 Error(错误)区别解析
Two ways to simulate multi-user login in Jmeter
Fluorescent marker peptides FITC/AMC/FAM/Rhodamine TAMRA/Cy3 / Cy5 / Cy7 - Peptide
MySQL 入门:Case 语句很好用
【Harmony OS】【ARK UI】ets使用startAbility或startAbilityForResult方式调起Ability
随机推荐
WebSocket的实际应用
The problem that the rosbag tool plotjuggler cannot open rosbag
Shell conditional statement judgment
【Harmony OS】【ArkUI】ets开发 基础页面布局与数据连接
接口管理工具YApi怎么用?颜值高、易管理、超好用
Interface test practice | Detailed explanation of the difference between GET / POST requests
【HMS core】【Ads Kit】华为广告——海外应用在国内测试正式广告无法展示
Shell之条件语句
Create a tree structure
多肽介导PEG磷脂——靶向功能材料之DSPE-PEG-RGD/TAT/NGR/APRPG
安装IIS服务(Internet信息服务(Internet Information Services,简写IIS,互联网信息服务)
GIS数据漫谈(五)— 地理坐标系统
How to prepare for the test interface test data
Practical application of WebSocket
Flink state
数字化时代,企业如何建立自身的云平台与商业模式的选择?
MySQL 出现 The table is full 的解决方法
Tag stack - stack monotonically preparatory knowledge - lt. 739. The daily temperature
内部类、static关键字、final
【Harmony OS】【FAQ】Hongmeng Questions Collection 1