【PAT】1024. Palindromic Number (25)
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)的更多相关文章
- PAT 甲级 1024 Palindromic Number (25 分)(大数加法,考虑这个数一开始是不是回文串)
1024 Palindromic Number (25 分) A number that will be the same when it is written forwards or backw ...
- 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 ...
- 【PAT甲级】1024 Palindromic Number (25 分)
题意: 输入两个正整数N和K(N<=1e10,k<=100),求K次内N和N的反置相加能否得到一个回文数,输出这个数和最小的操作次数. trick: 1e10的数字相加100次可能达到1e ...
- PAT 甲级 1024 Palindromic Number
1024. Palindromic Number (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...
- 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 ...
- 1024. Palindromic Number (25)
A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...
- 1024 Palindromic Number (25 分)
A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...
- PAT (Advanced Level) 1024. Palindromic Number (25)
手动模拟加法高精度. 注意:如果输入数字的就是回文,这个时候输出0步. #include<iostream> #include<cstring> #include<cma ...
- PAT甲题题解-1024. Palindromic Number (25)-大数运算
大数据加法给一个数num和最大迭代数k每次num=num+num的倒序,判断此时的num是否是回文数字,是则输出此时的数字和迭代次数如果k次结束还没找到回文数字,输出此时的数字和k 如果num一开始是 ...
随机推荐
- import xxx from 和 import {xxx} from的区别
1.vue import FunName from ‘../xxx’ 1.js export defualt function FunName() { return fetch({ url: '/ar ...
- 【LOJ】#2010. 「SCOI2015」小凸解密码
题解 断环为链,把链复制两份 用set维护一下全是0的区间,然后查找x + n / 2附近的区间,附近各一个过不去,最后弃疗了改为查附近的两个,然后过掉了= = 熟练掌握stl的应用,你值得拥有(雾 ...
- 【LOJ】#2085. 「NOI2016」循环之美
题解 我们要求的其实是这个东西= = \(\sum_{i = 1}^{n}\sum_{j = 1}^{n}[(i,j) == 1][(j,k) == 1]\) 然后变一下形 \(\sum_{j = 1 ...
- Deepin 2015 安装惠普打印机驱动
参考:https://bbs.deepin.org/forum.php?mod=viewthread&tid=34749&extra= 1.安装新立得包管理器:sudo apt-get ...
- Servlet技术——常用的类和接口
Servlet是运行在服务器端的Java应用程序,由Servlet容器对其进行管理. 当用户对容器发送HTTP请求时,容器将通知相应的Servlet对象进行处理,完成用户和程序之间的交互. 在Serv ...
- 扩展swap分区
swap分区在系统的物理内存不够用时,把硬盘的一部分空间释放出来,以供当前运行的程序使用.(临时使用,如果swap分区都不够了还是直接加内存吧) (1).步骤 mkswap /devices(可以是分 ...
- 选择器(UIPickerView)
Apple提供了两种形式的选择器:日期选择器(UIDatePicker)与自定义选择器(UIPickerView). 当用户使用日期选择器选定一个时间后,调用datePickerID.date可以直接 ...
- 01-学前入门.Net 能做什么
桌面应用程序 Winfrom(.Net开发的桌面应用程序叫Winfrom应用程序) Internet应用程序 ASP.NET (.Net开发的Internet应用程序叫ASP.N ...
- POJ3071 Football 概率DP 简单
http://poj.org/problem?id=3071 题意:有2^n个队伍,给出每两个队伍之间的胜率,进行每轮淘汰数为队伍数/2的淘汰赛(每次比赛都是相邻两个队伍进行),问哪只队伍成为冠军概率 ...
- [CC-ANUCBC]Cards, bags and coins
[CC-ANUCBC]Cards, bags and coins 题目大意: 给你\(n(n\le10^5)\)个数,\(q(q\le30)\)次询问,问从中选取若干个数使得这些数之和为\(m(m\l ...