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

分析:简单题。判断回文串。

#include<iostream>
#include<vector>
#include<string.h>
using namespace std; bool isPalindromic(vector<int> a)
{
int i,j;
bool flag = true;
for(i=0,j=a.size()-1; i<=j; i++,j--){
if(a[i] != a[j]){
flag = false;
break;
}
}
return flag;
} void show(vector<int> input)
{
int i;
for(i=input.size()-1; i>=0; i--)
{
cout<<input[i];
}
cout<<endl;
} int main()
{
vector<int> input;
int k;
char s[11];
scanf("%s %d",s,&k); int len = strlen(s);
int i;
for(i=0; i<len; i++)
input.push_back(s[i] - '0'); if(isPalindromic(input))
{
show(input); //输出
cout<<"0"<<endl;
}
else
{
int len_temp = len;
for(i=1; i<=k; i++){
int t = 0;
int end = len_temp - 1;
int start = 0;
int j = 0;
vector<int> vec_temp;
for(; start<len_temp && end>=0; start++,end--,j++){
vec_temp.push_back(t + input[start] + input[end]);
if(vec_temp[j] >= 10){
vec_temp[j] -= 10;
t = 1;
}
else
t = 0;
}
if(t > 0)
vec_temp.push_back(1);
len_temp = vec_temp.size();
input.assign(vec_temp.begin(),vec_temp.end());
if(isPalindromic(input))
break;
}
show(input);
if(i>k) cout<<k<<endl;
else
cout<<i<<endl;
}
return 0;
}

【PAT】1024. Palindromic Number (25)的更多相关文章

  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 Advanced 1024 Palindromic Number (25) [数学问题-⼤整数相加]

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

  3. 【PAT甲级】1024 Palindromic Number (25 分)

    题意: 输入两个正整数N和K(N<=1e10,k<=100),求K次内N和N的反置相加能否得到一个回文数,输出这个数和最小的操作次数. trick: 1e10的数字相加100次可能达到1e ...

  4. PAT 甲级 1024 Palindromic Number

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

  5. 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 ...

  6. 1024. Palindromic Number (25)

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

  7. 1024 Palindromic Number (25 分)

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

  8. PAT (Advanced Level) 1024. Palindromic Number (25)

    手动模拟加法高精度. 注意:如果输入数字的就是回文,这个时候输出0步. #include<iostream> #include<cstring> #include<cma ...

  9. PAT甲题题解-1024. Palindromic Number (25)-大数运算

    大数据加法给一个数num和最大迭代数k每次num=num+num的倒序,判断此时的num是否是回文数字,是则输出此时的数字和迭代次数如果k次结束还没找到回文数字,输出此时的数字和k 如果num一开始是 ...

随机推荐

  1. 尝试 TFS Express 2012.3

    之前一直使用SVN做版本管理,但是只能管理代码.之前的一份工作,只用了TFS来管理,可以将任务与代码集成管理,很是方便,只是安装太过于繁琐,现在的公司人少,不想费那么多事. 最关键的,就是安装TFS需 ...

  2. 获取对象的key【键】和分别获取数组的key【键】和值

    一.先说对象,如何获取key[键]: var obj={ name:"websong", qq:289483936 } 想要获取这个obj对象的键“name”和"qq&q ...

  3. Python爬虫笔记(一)

    个人笔记,仅适合个人使用(大部分摘抄自python修行路) 1.爬虫Response的内容 便是所要获取的页面内容,类型可能是HTML,Json(json数据处理链接)字符串,二进制数据(图片或者视频 ...

  4. phpstorm+xdebug远程调试设置

    1 xdebug扩展安装 1.1 xdebug扩展安装: 2 服务器PHP配置 3 phpstorm设置 3.1 添加远程debug 3.2 phpstorm设置: 4 浏览器插件安装 4.1 chr ...

  5. hihocoder 1490 Tree Restoration

    构造. 从最后一层开始往上构造.最后一层肯定都是叶子结点,距离为2的肯定是同一个父亲,确定好了父亲之后,可以确定上一层每个节点之间的距离,以及上一层每个节点到还未确定的叶子节点之间的距离. #incl ...

  6. Python之路【第五篇】: 函数、闭包、装饰器、迭代器、生成器

    目录 函数补充进阶 函数对象 函数的嵌套 名称空间与作用域 闭包函数 函数之装饰器 函数之可迭代对象 函数之迭代器 函数之生成器 面向过程的程序设计思想 一.函数进阶之函数对象 1. 函数对象 秉承着 ...

  7. 深度学习基础系列(二)| 常见的Top-1和Top-5有什么区别?

    在深度学习过程中,会经常看见各成熟网络模型在ImageNet上的Top-1准确率和Top-5准确率的介绍,如下图所示: 那Top-1 Accuracy和Top-5 Accuracy是指什么呢?区别在哪 ...

  8. [webpack] devtool里的7种SourceMap[转]

    modle: development cheap-source-map debug 不太方便,不是以原来的文件的形式cheap-module-source-map 可以 debugcheap-modu ...

  9. Week Five

    2018.12.25 1.[BZOJ 4310] 2.[BZOJ 3879] 3.[BZOJ 2754] 4.[BZOJ 4698] 5.[Codeforces 914E] 6.[Codeforces ...

  10. [GCJ2017R3]Cooclement

    题目大意: 一种数列按照如下方式变化: 新数列第i位等于原数中数字i的出现次数. 变化过程中数列长度不变. 例如数列12的变化过程为12-11-20-01-10. 现在告诉你一个数列x,请求出x可能是 ...