博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
1024. Palindromic Number (25)
阅读量:4955 次
发布时间:2019-06-12

本文共 2005 字,大约阅读时间需要 6 分钟。

A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit numbers are palindromic numbers.

Non-palindromic numbers can be paired with palindromic ones via a series of operations. First, the non-palindromic number is reversed and the result is added to the original number. If the result is not a palindromic number, this is repeated until it gives a palindromic number. For example, if we start from 67, we can obtain a palindromic number in 2 steps: 67 + 76 = 143, and 143 + 341 = 484.

Given any positive integer N, you are supposed to find its paired palindromic number and the number of steps taken to find it.

Input Specification:

Each input file contains one test case. Each case consists of two positive numbers N and K, where N (<= 1010) is the initial numer and K (<= 100) is the maximum number of steps. The numbers are separated by a space.

Output Specification:

For each test case, output two numbers, one in each line. The first number is the paired palindromic number of N, and the second number is the number of steps taken to find the palindromic number. If the palindromic number is not found after K steps, just output the number obtained at the Kth step and K instead.

Sample Input 1:
67 3
Sample Output 1:
4842
Sample Input 2:
69 3
Sample Output 2:
13533 代码:
#include 
#include
#include
#include
#include
#include
using namespace std;int main(){ char a[200],b[200]; int c = 0,d,i; cin>>a>>d; int len = strlen(a); strcpy(b,a); reverse(a,a+len); for(i = 0;i < d;i ++) { if(strcmp(a,b) == 0)break; for(int j = 0;j < len;j ++) { c += a[j] - '0' + b[j] - '0'; a[j] = c % 10 + '0'; c /= 10; } if(c) { a[len ++] = '0' + c % 10; c /= 10; a[len] = '\0'; } strcpy(b,a); reverse(a,a+len); } cout<
<
<

 

转载于:https://www.cnblogs.com/8023spz/p/7944557.html

你可能感兴趣的文章
x86 的 TSS 任务切换机制
查看>>
Github 提交本地代 到远程 菜鸟 简明教程
查看>>
原型模式
查看>>
Git 统计某作者代码量
查看>>
js去除重复字符串两种实现方法
查看>>
mysql之binlog
查看>>
linux文件的通用操作方法学习
查看>>
最小化安装CentOS7 + xfce4 +PHP + nginx +mariadb 开发环境
查看>>
android图像处理系列之五-- 给图片添加边框(中)
查看>>
Android Animation学习(三) ApiDemos解析:XML动画文件的使用
查看>>
Activity切换动画(overridePendingTransition)-翻页效果
查看>>
double类型相减有小数误差
查看>>
Scalaz(52)- scalaz-stream: 并行运算-parallel processing concurrently by merging
查看>>
Hyper-V创建固定大小虚拟机
查看>>
centos下kill、killall、pkill命令区别
查看>>
gitblit git SERVER window 安装配置 hook post-receive 自动部署
查看>>
【11】specified value,computed value,used value计算方法
查看>>
myeclipse中出现The method xxx of type must override or implement a supertype
查看>>
fzu 2111 Min Number
查看>>
Python开发【第六篇】:模块
查看>>