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. 20155321 2016-2017-2 《Java程序设计》第七周学习总结

    20155321 2016-2017-2 <Java程序设计>第七周学习总结 教材学习内容总结 Date/DateFormat Date是日期类,可以精确到毫秒. 构造方法 Date() ...

  2. HDU 1242 Rescue (广搜)

    题目链接 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The priso ...

  3. NodeJS让前端与后端更友好的分手

    学问   最近“上层建筑”在兴起国学热,所以公司几个月前决定开发一款名叫“学问”的有关于国学的app.     APP的详情页面还是由web来显现具体内容,有些类似于新闻页,图文混排什么的web是最适 ...

  4. jQuery文档处理(追加删除)——(三)

    1.追加内容

  5. Spring4笔记8--Spring与JDBC模板(IoC应用的例子)

    Spring 与 JDBC模板: 为了避免直接使用 JDBC 而带来的复杂且冗长的代码,Spring 提供了一个强有力的模板类---JdbcTemplate 来简化 JDBC 操作.并且,数据源 Da ...

  6. plsql链接数据库配置

    一. 目录结构 D:\install\PLSQL        |-- instantclient_11_2            |-- tnsnames.ora        |-- PLSQL ...

  7. 5 - django-csrf-session&cookie

    目录 1 CSRF跨站请求伪造 1.1 CSRF攻击介绍及防御 1.2 防御CSRF攻击 1.2.1 验证 HTTP Referer 字段 1.2.2 在请求地址中添加 token 并验证 1.2.3 ...

  8. 搭建本地git服务器

    最近因为项目需求,需要实现一个原型系统,加上后期项目需要多人协作,考虑采用了git做版本控制. 这里主要简要描述下git服务器和客户端的搭建和配置. 1.git服务器 (1)安装git sudo ap ...

  9. django Rest Framework----GenericAPIView 通用视图 GenericAPIView源码分析

    一.GenericAPIView GenericAPIView扩展了APIView,为标准列表和详细视图添加了常见的行为. 提供的每个具体通用视图都是一个GenericAPIView或多个mixin类 ...

  10. ggplot2使用初探

    ggplot2已经成为了R语言中数据可视化的同义词, 这是一个强大的工具, 可以帮助我们制作优良的图表, 创造出令人吃惊的图片, 下面我们一起学习(本博文参考了知乎问题如何使用 ggplot2中黄宝臣 ...