problem

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 (<= 10^10^) 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: 484
2
Sample Input 2: 69 3
Sample Output 2: 1353
3

tip

answer

#include<iostream>

#define LL long long
using namespace std; LL num, Time; LL Palin(LL a){
LL b = 0, t = 0;
t = a;
while(t > 0){
b *= 10;
b += t%10;
t /= 10;
}
return b;
} LL Add(LL a, LL &t){
t++;
return a+Palin(a);
} int main() {
// freopen("test.txt", "r", stdin);
scanf("%lld%lld", &num, &Time);
LL t = 0;
if(num == 0) {
printf("0\n0");
return 0;
}
LL tn = num;
while(tn != Palin(tn)){
tn = Add (tn, t);
if(t >= Time) break;
}
printf("%lld\n%lld", tn, t);
return 0;
}

experience

  • Palindromic 回文的

  • pat 的判题器不支持long long I64d, 支持lld, printf时应该使用 lld。

1024 Palindromic Number (25)(25 point(s))的更多相关文章

  1. 1019 General Palindromic Number (20)(20 point(s))

    problem A number that will be the same when it is written forwards or backwards is known as a Palind ...

  2. PAT 甲级 1024 Palindromic Number (25 分)(大数加法,考虑这个数一开始是不是回文串)

    1024 Palindromic Number (25 分)   A number that will be the same when it is written forwards or backw ...

  3. A1082 Read Number in Chinese (25)(25 分)

    A1082 Read Number in Chinese (25)(25 分) Given an integer with no more than 9 digits, you are suppose ...

  4. PAT 甲级 1024 Palindromic Number

    1024. Palindromic Number (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...

  5. PAT 甲级 1006 Sign In and Sign Out (25)(25 分)

    1006 Sign In and Sign Out (25)(25 分) At the beginning of every day, the first person who signs in th ...

  6. 【PAT】1020 Tree Traversals (25)(25 分)

    1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...

  7. 【PAT】1052 Linked List Sorting (25)(25 分)

    1052 Linked List Sorting (25)(25 分) A linked list consists of a series of structures, which are not ...

  8. 【PAT】1060 Are They Equal (25)(25 分)

    1060 Are They Equal (25)(25 分) If a machine can save only 3 significant digits, the float numbers 12 ...

  9. 【PAT】1032 Sharing (25)(25 分)

    1032 Sharing (25)(25 分) To store English words, one method is to use linked lists and store a word l ...

  10. 1003 Emergency (25)(25 point(s))

    problem 1003 Emergency (25)(25 point(s)) As an emergency rescue team leader of a city, you are given ...

随机推荐

  1. 20155315 2016-2017-2 《Java程序设计》第五周学习总结

    教材学习内容总结 第8章 异常处理 1.使用try...catch 与C语言中程序流程和错误处理混在一起不同,Java中把正常流程放try块中,错误(异常)处理放catch块中. 如果父类异常对象在子 ...

  2. 20155330 2016-2017-2 《Java程序设计》第五周学习总结

    20155330 2016-2017-2 <Java程序设计>第五周学习总结 教材学习内容总结 学习目标 理解异常架构 掌握try...catch...finally处理异常的方法 会用t ...

  3. Python练习-从小就背不下来的99乘法表

    心血来潮,灵机一动,反正就是无聊的做了一个很简单的小玩意: for i in range(1,10):#让i 1-9 循环9次 print("\n")#每循环一次进行一次换行 fo ...

  4. SQLServer数据操作(建库、建表以及数据的增删查改)[转]

    SQLSever数据操作   一.建立数据库:   create database DB ---数据库名称 (          name=data1 --文件名,          filename ...

  5. 1 - django-介绍-MTV-命令-基础配置-admin

    目录 1 什么是web框架 2 WSGI 3 MVC与MTV模式 3.1 MVC框架 3.2 MTV框架 3.3 区别 4 django介绍 4.1 Django处理顺序 4.2 创建django站点 ...

  6.  Meltdown论文翻译【转】

    转自:http://www.wowotech.net/basic_subject/meltdown.html#6596 摘要(Abstract) The security of computer sy ...

  7. mysql5.7.10 源码编译安装记录 (centos6.4)【转】

    一.准备工作 1.1 卸载系统自带mysql 查看系统是否自带MySQL, 如果有就卸载了, 卸载方式有两种yum, rpm, 这里通过yum卸载 rpm -qa | grep mysql //查看系 ...

  8. if(a==1) & if(1==a) 区别

    [前提] 在公司参加项目时,看到前辈写if比较数值是否相等,经常会写 if(1==a) ,觉得有些奇怪,如是乎,将调查结果写下来记录一下. [结果] if(a==1) 与 if(1==a)是没有什么区 ...

  9. C++模板(Templates)

    模板(template)是泛型编程的基础,"泛型"的含义就是没有不依赖具体的数据类型.模板的引入是为了创建一般性的类(模板类)或者函数(模板函数).典型的容器比如迭代器/算法等是泛 ...

  10. thinkphp5 IIS7.5 隐藏index.php的方法

    <?xml version="1.0" encoding="UTF-8"?> <configuration> <system.we ...