1024. Palindromic Number (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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:

484
2

Sample Input 2:

69 3

Sample Output 2:

1353
3
由于数字会超longlong int 所以用数组
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <map>
#include <string> using namespace std;
typedef long long int LL;
char b[20005];
int a[20005];
int c[20005];
int k;
int len;
int add()
{
int i=0;
int j=0;
int num=0;
while((i<len&&j<len)||num!=0)
{
a[i]=(a[i]+c[j]+num);
num=a[i]/10;
a[i]%=10;
i++;
j++;
}
return i;
}
void fun()
{
int cnt=0;
for(int i=len-1;i>=0;i--)
c[cnt++]=a[i];
}
int judge()
{
int i=0;int j=len-1;
while(i<=j)
{
if(a[i]!=a[j])
return 0;
i++,j--;
}
return 1;
}
void output(int x)
{
for(int i=len-1;i>=0;i--)
printf("%d",a[i]);
cout<<endl;
cout<<x<<endl;
}
int main()
{
scanf("%s",b);
scanf("%d",&k);
int l=strlen(b);
len=l;
for(int i=0;i<len;i++)
a[i]=b[i]-'0';
if(judge())
{
output(0);
return 0;
} for(int i=1;i<=k;i++)
{
fun();
len=add();
if(judge())
{
output(i);
return 0;
}
}
output(k);
return 0;
}

PAT 甲级 1024 Palindromic Number的更多相关文章

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

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

  2. 【PAT】1024. Palindromic Number (25)

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

  3. PAT甲级——A1024 Palindromic Number

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

  4. PAT Advanced 1024 Palindromic Number (25) [数学问题-⼤整数相加]

    题目 A number that will be the same when it is written forwards or backwards is known as a Palindromic ...

  5. PAT 1024 Palindromic Number[难]

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

  6. 1024 Palindromic Number (25)(25 point(s))

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

  7. 1024 Palindromic Number int_string转换 大整数相加

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

  8. PAT A1019 General Palindromic Number (20 分)——回文,进制转换

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

  9. 1024. Palindromic Number (25)

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

随机推荐

  1. Ubuntu环境下使用npm安装node模块时报错的处理方法

    错误信息: npm ERR : node: not found : npm ERR! not ok code 0 解决方案: sudo apt-get install nodejs-legacy 也可 ...

  2. wget镜像网站并且下载到指定目录 2012-06-20 19:40:56

    wget镜像网站并且下载到指定目录 2012-06-20 19:40:56 分类: Python/Ruby wget -r -p -np -k -P /tmp/ap http://www.exampl ...

  3. iOS应用安全防护框架概述

    iOS应用安全防护框架概述 攻易防难,唯有缜密.多层的防护网络才能可靠的保护我们iOS应用程序的安全.那么,一个完善的iOS应用安全防护框架都要写哪些东西呢? 首先,先梳理一下常见的逆向及攻击工具. ...

  4. Python 内置模块函数filter reduce

    1.filter()实现过滤的功能 2.reduce()对序列中元素的连续操作可以通过循环来处理 3.map()对tuple元组进行解包操作,调用时设置map()的第一个参数为None 4.使用red ...

  5. jquery 获取各种高宽

    获取浏览器显示区域(可视区域)的高度 :   $(window).height();   获取浏览器显示区域(可视区域)的宽度 : $(window).width();   获取页面的文档高度   $ ...

  6. 基于jquery的适合电子商务网站首页的图片滑块

    今天给大家分享一款基于Sequence.js 的图片滑动效果,特别适合电子商务网站或者企业产品展示功能.带有图片缩率图,能够呈现全屏图片浏览效果.结合 CSS3 Transition 实现响应式的滑块 ...

  7. 16V554 的测试代码

    //------------------------------------------------------------------------ #include   "AT16C554 ...

  8. HTTP Content-Disposition Explanation [ from MDN ]

    在常规的HTTP应答中,Content-Disposition 消息头指示回复的内容该以何种形式展示,是以内联的形式(即网页或者页面的一部分),还是以附件的形式下载并保存到本地. 在multipart ...

  9. easy UI动态赋值

    1,首先怎么清除这个值 $('#filegrid').datagrid('loadData', { total: 0, rows: [] }); 2,清除后,通过post提交请求,怎么将新植穿进去,这 ...

  10. AJAX的中文乱码问题

    /***********本人原创,欢迎转载,转载请保留本人信息*************/作者:wallimn电邮:wallimn@sohu.com博客:http://blog.csdn.net/wa ...