PAT甲级1024. Palindromic Number (25)
来源:互联网 发布:厦门淘宝运营培训 编辑:程序博客网 时间:2024/05/29 04:02
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
所给整数N反过来之后可能会溢出;用string表示整数N,reverse非常方便。
#include <iostream>#include <string>#include <algorithm>using namespace std;string add(string n,string m){ string result; int carry=0; for(int i=n.size()-1;i>=0;i--){ char c=n[i]+m[i]-'0'+carry; carry=0; if(c>'9'){ carry=1; c-=10; } result.push_back(c); } if(carry) result.push_back('1'); reverse(result.begin(),result.end()); return result;}int main(){ string N; int K; cin>>N>>K; int step=0; while(step<K){ string m=N; reverse(m.begin(),m.end()); if(m==N) break; N=add(N,m); step++; } cout<<N<<'\n'<<step<<endl; return 0;}
- 【PAT甲级】1024. Palindromic Number (25)
- 1024. Palindromic Number (25)PAT甲级
- PAT甲级练习1024. Palindromic Number (25)
- PAT甲级1024. Palindromic Number (25)
- PAT 甲级 1024. Palindromic Number (25)
- PAT甲级 1024. Palindromic Number (25)
- PAT 甲级 1024. Palindromic Number
- PAT甲级练习题A1024. Palindromic Number (25)
- PAT 甲级 1024 Palindromic Number
- 【PAT】1024. Palindromic Number (25)
- PAT 1024. Palindromic Number (25)
- 【PAT】1024. Palindromic Number (25)
- PAT 1024. Palindromic Number (25)
- PAT 1024. Palindromic Number (25)
- pat 1024. Palindromic Number (25)
- PAT 1024. Palindromic Number (25)
- 1024. Palindromic Number (25)-PAT甲级真题(大整数相加)
- PAT甲级.1019. General Palindromic Number (20)
- java 多线程同步机制
- MySQL 关于日志的一些配置参数总结
- 继承
- 51nod 1681 公共祖先 主席树dfs序
- Android中使用开源框架EventBus3.0实现Fragment之间的通信交互
- PAT甲级1024. Palindromic Number (25)
- 看大神是如何使用jstack跟踪异常代码
- Web前端实战及经验收集
- 搭建Maven环境
- C++全局和局部随机洗牌算法
- 最右技术问答的一点个人见解
- error LNK2019: 无法解析的外部符号 _WinMain@16,该符号在函数 ___tmainCRTStartup 中被引用
- docker常用指令简介(干货)
- php设计模式之装饰器