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 (<= 10^10^) 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
题目大意就是:给出一个大数,并将其反转,在有限次k次相加内,若结果是回文数,那么就输出并且输出计算次数;否则就直接输出计算k次的结果。
注意到n可能会非常大,10的10次方,而且要进行k次(最多100次),所以用long long不可以了,用string来做大数运算,并且string有reverse函数。
还有主函数中的逻辑判断问题,因为一个数如果是回文数,那么运算结果肯定也是回文数,只要判断一个数反转后是否等于自身,如果相等,那么
就已经找到回文数,就break即可。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<stdlib.h>
#include<algorithm>
using namespace std;
//
string add(string s1,string s2){
long long len=s1.size();
int a,b=;
for(int i=;i<len;i++){
if(b==)
a=(s1[i]-'')+(s2[i]-'');
else{
a=(s1[i]-'')+(s2[i]-'')+;
b=;
}
if(a>=){
b=;
a%=;
}
s2[i]=a+'';
}
if(b==){
s2=s2+'';
}
reverse(s2.begin(),s2.end());
return s2;
}
bool isH(string s){
long long len=s.size();
long long f=len/;
bool flag=true;
for(int i=;i<f;i++)
if(s[i]!=s[len-i-]){
flag=false;break;
}
return flag;
}
int main()
{
string s;
int k;
cin>>s>>k;
string s2;
int n=;
bool flag=false;
while(n<k){
s2=s;
reverse(s.begin(),s.end());
if(s2==s){
break;
}else{
s=add(s,s2);
n++;
}
}
cout<<s<<'\n'<<n;
return ;
}
 

PAT 1024 Palindromic Number[难]的更多相关文章

  1. PAT 1024 Palindromic Number

    #include <cstdio> #include <iostream> #include <cstdlib> #include <algorithm> ...

  2. PAT 甲级 1024 Palindromic Number

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

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

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

  4. 【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 ...

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

  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 A1024 Palindromic Number (25 分)——回文,大整数

    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. 【基础知识】列一下一个.Net WEB程序员需要掌握的知识

    基础部分 C# 基础语法 OOP的概念,面向对象的理解 继承 封装 多态 ASP.NET MVC (Web Form 用的越来越少,如果你不熟悉,可以不看) JavaScript 基础语法 如何在HT ...

  2. 重载i++,++i操作符

    #include <iostream> using namespace std; class Time { public: Time(){min=;sec=;} Time(int m,in ...

  3. [BeiJing2011]元素[贪心+线性基]

    2460: [BeiJing2011]元素 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 1245  Solved: 652[Submit][Stat ...

  4. Rails secret token bug

    ArgumentError (A secret is required to generate an integrity hash for cookie session data. Use confi ...

  5. Dokcer制作nginx镜像,提交镜像至仓库

    生成Dockerfile FROM docker.io/hagaico/centos-base-6.5:latest MAINTAINER yatho yatho@163.com ENV DEBIAN ...

  6. Jquery操作select选项集合,判断集合中是否存在option

    转载:http://www.cnblogs.com/pepcod/archive/2012/07/03/JavaScript.html Query获取Select选择的Text和Value: 语法解释 ...

  7. WCF之HTTPS传输WebConfig配置

    <system.serviceModel> <client> <endpoint address="https://evitest.1010bao.com:88 ...

  8. Java工程师之SpringBoot系列教程前言&目录

    前言 与时俱进是每一个程序员都应该有的意识,当一个Java程序员在当代步遍布的时候,你就行该想到我能多学点什么.可观的是后端的框架是稳定的,它们能够维持更久的时间在应用中,而不用担心技术的更新换代.但 ...

  9. 使用pidstat监控资源使用

    linux可以使用pidstat命令监控系统资源,比如监控cup使用如下: pidstat -u 还可以使用 -r(内存) -d(硬盘)

  10. 金字塔原理——MECE(Mutually Exclusive Collectively Exhaustive)

    一.金字塔原理 它的样子: 一个中心思想,分出下面2到N个思想支撑,每个分论点下面又有2到N个思想(事实或数据)支撑,以此类推,形状如金字塔.原则是以终为始(先结果后原因),以上统下,归纳分组,逻辑递 ...