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 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 nonpalindromic 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 afer 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(<=10^10),最大操作次数K(<=100),若整数不为回文数,进行如下操作(1. 反转N得到FN;2.N=FN+N),循环执行,直到操作次数达到K次或者N成为回文数
N若为最大值1010操作100次大于1020,超过long long类型范围,需要看做大整数处理数据(string)
解题思路
思路1
- 判断N是否为回文数,如果不是回文数,进入循环操作,直到操作次数达到k或者N成为回文数
- 大整数处理,需要逆置字符串,因为操作从数字的个位开始(但是a,b互为反转,所以本题中只需要反转一个就可以)
思路2
高精度加法
Code
Code 01
#include <iostream>
#include <cmath>
#include <string>
#include <algorithm>
using namespace std;
bool isPac(string & s) {
for(int i=0; i<s.length(); i++) {
if(i>=s.length()-1-i)break;
if(s[i]!=s[s.length()-1-i])return false;
}
return true;
}
string bignadd(string a) {
// b是a的反转 长度一定相等,无需考虑不相等的情况
string b = a;
reverse(b.begin(),b.end());
int carry=0;
string c;
for(int i=0; i<a.length(); i++) {
int tempa = a[i]-'0',tempb = b[i]-'0';
int temp = tempa+tempb+carry;
c.push_back(temp%10+'0');
carry=temp/10;
}
if(carry==1)c.push_back('1');
reverse(c.begin(),c.end());
return c;
}
int main(int argc,char *argv[]) {
string n,temp;
int m;
cin>>n>>m;
temp = n;
int i=1;
for(i=1; i<=m&&!isPac(temp); i++) {
temp = bignadd(temp);
}
printf("%s\n",temp.c_str());
printf("%d",i-1);
return 0;
}
Code 02(高精度加法)
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
/*
高精度加法
*/
struct bign{
int d[1000];
int len;
bign(){
memset(d,0,sizeof(d));
len = 0;
}
};
bign change(char str[]){
bign a;
a.len=strlen(str);
for(int i=0;i<a.len;i++){
a.d[i]=str[a.len-1-i]-'0';
}
return a;
}
bign add(bign a,bign b){
bign c;
int r = 0;
for(int i=0;i<a.len;i++){
int temp = a.d[i]+b.d[i]+r;
c.d[c.len++]=temp%10;
r = temp/10;
}
if(r!=0)c.d[c.len++]=r;
return c;
}
bool judge(bign a){
for(int i=0;i<=a.len/2;i++){
if(a.d[i]!=a.d[a.len-1-i]){
return false;
}
}
return true;
}
void print(bign a){
for(int i=a.len-1;i>=0;i--){
printf("%d",a.d[i]);
}
printf("\n");
}
int main(int argc,char * argv[]){
char s[1000];
int k,t=0;
scanf("%s %d",s,&k);
bign a = change(s);
while(!judge(a)&&t<k){
bign b = a;
// reverse
reverse(b.d,b.d+b.len);
// s = s+r
a = add(a,b);
t++;
}
print(a);
printf("%d\n",t);
return 0;
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 趁着过年的时候手搓了一个低代码框架
· 推荐一个DeepSeek 大模型的免费 API 项目!兼容OpenAI接口!