【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一开始是 ...
随机推荐
- 尝试 TFS Express 2012.3
之前一直使用SVN做版本管理,但是只能管理代码.之前的一份工作,只用了TFS来管理,可以将任务与代码集成管理,很是方便,只是安装太过于繁琐,现在的公司人少,不想费那么多事. 最关键的,就是安装TFS需 ...
- Python对Excel的操作
Python几个读取Excel库的介绍: xlwings 可结合 VBA 实现对 Excel 编程,强大的数据输入分析能力,同时拥有丰富的接口,结合 pandas/numpy/matplotlib 轻 ...
- NEO4j简单入门
Neo4j是: 一个开源 无Schema 没有SQL 图形数据库 图形数据库也称为图形数据库管理系统或GDBMS. Neo4j的官方网站:http://www.neo4j.org Neo4j的优点 它 ...
- ARP监测工具Arpwatch
ARP监测工具Arpwatch ARP协议是网络的基础协议.基于ARP协议的ARP攻击是局域网最为常见和有效的攻击方式.ARP攻击可以通过发送伪造的ARP包实施欺骗,实现各种中间人攻击.Arpwa ...
- CF1051D Bicolorings dp
水题一道 $f[i][j][S]$表示$2 * i$的矩形,有$j$个联通块,某尾状态为$S$ 然后转移就行了... #include <vector> #include <cstd ...
- POJ 3904 JZYZOJ 1202 Sky Code 莫比乌斯反演 组合数
http://poj.org/problem?id=3904 题意:给一些数,求在这些数中找出四个数互质的方案数. 莫比乌斯反演的式子有两种形式http://blog.csdn.net/out ...
- SEL和IMP
http://www.jianshu.com/p/4a09d5ebdc2c SEL : 类成员方法的指针,但不同于C语言中的函数指针,函数指针直接保存了方法的地址,但SEL只是方法编号. IMP:一个 ...
- Velocity模板学习(一)
一.Velocity是什么 Velocity是一个基于Java的模板引擎,允许任何人仅仅简单地使用模板语言就可以引用由Java代码编写的对象. 二.Velocity的基本语法 1.变量 变量的定义 在 ...
- C# 7.0中可能出现的语法
今天在MSDN上看到的微软关于微软关于C# 7.0特性的Work List,主要特性如下: Tuple增强 Tuple的可读性一直不是很好, 很多时候宁愿新写一个类也不使用Item1, Item2这种 ...
- Spark编程指南V1.4.0(翻译)
Spark编程指南V1.4.0 · 简单介绍 · 接入Spark · Spark初始化 · 使用Shell · 在集群上部署代码 ...