1024. Palindromic Number (25)

时间限制
400 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 "algorithm"
#include "string"
#include <string.h>
#include<stdlib.h>
#include<sstream>
#include "vector"
using namespace std;

string add(string s1,string s2)
{
int n = s1.length();
int sum;
string result = "";
int carry = 0;
for(int i=n-1;i>=0;i--)
{
sum = (s1[i]-'0')+(s2[i]-'0');
sum += carry;
result.insert(result.begin(),(sum%10)+'0');
carry = sum/10;
}
if(carry)
{
result.insert(result.begin(),carry+'0');
}
return result;
}

int main()
{
string str;
string rstr;
int n;
cin >> str >> n;
int t = 0;
while(t<n)
{
rstr = str;
reverse(rstr.begin(),rstr.end());
if(rstr == str)
break;
else
str= add(rstr,str);
t++;
}
cout << str <<endl;
cout << t <<endl;

return 0;
}


浙大 pat 1024题解的更多相关文章

  1. 浙大pat 1035题解

    1035. Password (20) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue To prepare f ...

  2. 浙大pat 1025题解

    1025. PAT Ranking (25) 时间限制 200 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Programmi ...

  3. 浙大pat 1011题解

    With the 2010 FIFA World Cup running, football fans the world over were becoming increasingly excite ...

  4. 浙大PAT 7-06 题解

    #include <stdio.h> #include <iostream> #include <algorithm> #include <math.h> ...

  5. 浙大pat 1012题解

    1012. The Best Rank (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue To eval ...

  6. 浙大 pat 1003 题解

    1003. Emergency (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...

  7. 浙大 pat 1038 题解

    1038. Recover the Smallest Number (30) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...

  8. 浙大 pat 1047题解

    1047. Student List for Course (25) 时间限制 400 ms 内存限制 64000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  9. 浙大pat 1054 题解

    1054. The Dominant Color (20) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard Behind the scen ...

随机推荐

  1. [coding horror] 1 - sum 2

    sum 2 描述 输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的. 对应每个测试案例,输出两个数,小的先输出. codi ...

  2. Linode VPS上架日本东京2号机房,性能速度评测报告(推荐)

    我非常喜欢的海外免备案vps品牌linode日本机房长期缺货,中国用户想买都买不到.不过近日,陆续有国内朋友收到了Linode邀请,Tokyo 2日本东京机房开启内测,很快正式上架销售. 苦等太久的站 ...

  3. Java中不定参的使用规则

    Java中有时候会使用到不定参数,它的使用规则有2项: 一个方法中只能使用一个不定参数. 不定参数必须是方法中最后一个参数. 不定参数在传入的过程中会行成一个数组传入,如果不会放在最后一个,虚拟机无法 ...

  4. 【转】python删除文件里包含关键词的行

    import shutil with open('/path/to/file', 'r') as f: with open('/path/to/file.new', 'w') as g: for li ...

  5. android之活动状态、生存期、启动模式

    活动状态:1.运行状态2.暂停状态3.停止状态4.销毁状态 活动的生存期 七个回调方法1.onCreate()2.onStart()3.onResume()4.onPause()5.onStop()6 ...

  6. Android中Activity全局共享方法AppContext

    在 android 的所有 activity 中共享同一个对象实例的方法 1. 新建 AppContext.java 文件(名称可自定义) public class AppContext extend ...

  7. CSS 待解决问题

    记录一些待解决的问题 1. 关于 table 边框参差不齐问题 乍一看表头的边框有 间隙, 下边框也有点没有对其的问题. 解决方案:  给 table 添加: border-collapse: col ...

  8. MVC 的 视图中 @section 是什么作用?

    可以定义一个渲染块,这个渲染块可以在LayoutPage里面引用,使用Html.RenderSection("section名称"); 可以指定一个bool参数指定如果Conten ...

  9. C# 语言规范_版本5.0 (第16章 异常)

    1. 异常 C# 中的异常用于处理系统级和应用程序级的错误状态,它是一种结构化的.统一的和类型安全的处理机制.C# 中的异常机制非常类似于 C++ 的异常机制,但是有一些重要的区别: 在 C# 中,所 ...

  10. 解决gstreamer无法播放的bug

    0 ls 1 ./start.sh 2 ps 3 kill 366 4 cd /usr/app/services/ 5 ./start.sh 6 cd ../ 7 ls 8 cd res/ 9 ls ...