【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一开始是 ...
随机推荐
- 【Spark亚太研究院系列丛书】Spark实战高手之路-第3章Spark架构设计与编程模型第3节:Spark架构设计(2)
三,深入RDD RDD本身是一个抽象类,具有很多具体的实现子类: RDD都会基于Partition进行计算: 默认的Partitioner如下所示: 其中HashPartitioner的文档说明如下: ...
- (10) go 错误
没有 try catch..f.. 自定义错误
- Mac 命令行美化
在 mac 中使用原生的命令行工具,竟然没有 git 命令的自动补全,在 git 仓库下也看不到当前的分支名,不能忍.于是,开始一波改造. 目标:命名 Tab 自动补全:可以显示分支名: 一番 Goo ...
- Java常用工具类之时间转换(注释乱码,全)
package com.wazn.learn.util; import java.text.ParseException; import java.text.SimpleDateFormat; imp ...
- 【并查集】BZOJ4668-冷战
[题目大意] 给出N个军工厂和M 个操作,操作分为两类: • 0 u v,这次操作苏联会修建一条连接 u 号军工厂及 v 号军工厂的铁路,注意铁路都是双向的; • 1 u v, Reddington ...
- Python入门,以及简单爬取网页文本内容
最近痴迷于Python的逻辑控制,还有爬虫的一方面,原本的目标是拷贝老师上课时U盘的数据.后来发现基础知识掌握的并不是很牢固.便去借了一本Python基础和两本爬虫框架的书.便开始了自己的入坑之旅 言 ...
- hdu 3046 最小割
每个栅栏其实就是一条边,修一些栅栏,使得狼不能抓到羊,其实就是求一个割,使得羊全在S中,狼全在T中. #include <cstdio> #include <cstring> ...
- bozj 1449/2895: 球队预算 -- 费用流
2895: 球队预算 Time Limit: 10 Sec Memory Limit: 256 MB Description 在一个篮球联赛里,有n支球队,球队的支出是和他们的胜负场次有关系的,具体 ...
- python开发_textwrap文本样式
在看python的API的时候,发现python的textwrap在处理字符串样式的时候功能强大 在这里我做了一个demo: textwrap提供了一些方法: wrap(text, width = 7 ...
- [转]Android Service完全解析,关于服务你所需知道的一切
目录(?)[+] Android Service完全解析,关于服务你所需知道的一切(上) 分类: Android疑难解析2013-10-31 08:10 6451人阅读 评论(39) 收藏 举报 ...