【PAT刷题甲级】1136.A Delayed Palindrome

news/2024/6/3 17:21:46 标签: c++, PAT, 数据结构, 大整数相加

1136 A Delayed Palindrome (20 分)

Consider a positive integer N written in standard notation with k+1 digits ai as ak ⋯a1 a0 with 0≤ai <10 for all i and ak>0. Then N is palindromic if and only if ai =ak−i for all i. Zero is written 0 and is also palindromic by definition.

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. Such number is called a delayed palindrome. (Quoted from https://en.wikipedia.org/wiki/Palindromic_number )

Given any positive integer, you are supposed to find its paired palindromic number.

Input Specification

Each input file contains one test case which gives a positive integer no more than 1000 digits.

Output Specification

For each test case, print line by line the process of finding the palindromic number. The format of each line is the following:

A + B = C

where A is the original number, B is the reversed A, and C is their sum. A starts being the input number, and this process ends until C becomes a palindromic number – in this case we print in the last line C is a palindromic number.; or if a palindromic number cannot be found in 10 iterations, print Not found in 10 iterations. instead.

Sample Input 1

97152

Sample Output 1

97152 + 25179 = 122331
122331 + 133221 = 255552
255552 is a palindromic number.

Sample Input 2

196

Sample Output 2

196 + 691 = 887
887 + 788 = 1675
1675 + 5761 = 7436
7436 + 6347 = 13783
13783 + 38731 = 52514
52514 + 41525 = 94039
94039 + 93049 = 187088
187088 + 880781 = 1067869
1067869 + 9687601 = 10755470
10755470 + 07455701 = 18211171
Not found in 10 iterations.

题意

本题考查大整数相加以及判断是否为回文数。
若输入的数不是回文数,那么将其逆转再相加,判断和是否为回文数,是则输出,不是则重复上述过程。迭代次数不能超过10次。
PS:注意一开始就要判断输入的数是否为回文数。
另:大整数相加有参考博主:昵称五个字【PAT】A1136 A Delayed Palindrome【大整数相加

代码

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
string add(string a,string b) {	//大整数相加
	string c=a;
	int carry=0,len=a.length();	//carry为进位
	for(int i=len-1; i>=0; i--) {
		int tmp = (a[i]-'0') + (b[i]-'0') + carry;
		c[i] = tmp % 10 + '0';
		carry = tmp / 10;
	}
	if(carry!=0) {	//若最后仍有进位,直接加在结果的最前面
		c.insert(c.begin(),carry+'0');
	}
	return c;
}
bool ispnum(string s) {
	int j=s.length()-1;
	for(int i=0; i<s.length() && j>=0; i++) {
		if(s[i]!=s[j]) {
			return false;
		}
		j--;
	}
	return true;
}
int main() {
	string a,b;
	cin >> b;
	a=b;
	reverse(b.begin(),b.end());
	string c=add(a,b);
	if(ispnum(a)) {
		printf("%s is a palindromic number.",a.c_str());
		return 0;
	}
	printf("%s + %s = %s\n",a.c_str(),b.c_str(),c.c_str());
	int cnt=1;
	while(!ispnum(c) && cnt<10) {
		string t2=c;
		string t1=t2;
		reverse(t2.begin(),t2.end());
		c=add(t1,t2);
		printf("%s + %s = %s\n",t1.c_str(),t2.c_str(),c.c_str());
		cnt++;
	}
	if(cnt==10 && !ispnum(c))	printf("Not found in 10 iterations.");
	else	printf("%s is a palindromic number.",c.c_str());

	return 0;
}

http://www.niftyadmin.cn/n/1164391.html

相关文章

【PAT刷题甲级】1137.Final Grading

1137 Final Grading (25 分) 针对测试点3答案错误&#xff0c;已解决&#xff01; For a student taking the online course “Data Structures” on China University MOOC (http://www.icourse163.org/), to be qualified for a certificate, he/she must first obtain no l…

Blender shape key制作表情

https://www.youtube.com/watch?vYDu6y_2jFg0 新建basis作为基础shape (每个模型都必须要有一个基础shape)1是添加shape key2是重置为0 新建一个shape key 把value拉到1开始雕刻表情 雕完就可以了&#x1f60f; 导入的时候Unity会自动导入的 导出fbx的时候就有动画

Maya Zbrush goZ插件的安装

原来Zbrush可以自动安装的&#xff0c;不过文档有些过时了 重置为maya 设置maya exe 的路径 就可以自动安装了

C# virtual 重写和 new重写的区别

virtual重写主要是为了实现多态&#xff0c;比如张三和李四都有一个射击行为(父类射击是直线射击)&#xff0c;张三实现一个父类重写里面的射击&#xff0c;射出的转圈的子弹&#xff0c;而李四重写父类的射击&#xff0c;射出的是弧形的子弹 virtual和new 的结果是一样的&…

为什么建模要尽量用四边面

https://www.turbosquid.com/3d-modeling/training/modeling/tris-quads-n-gons/ https://www.pluralsight.com/blog/film-games/ngons-triangles-bad 建模四边面已经是工业认可的标准(动画、建模)四边面可以避免很多问题&#xff0c;如&#xff1a;动画的拉伸变形&#xff0c;…

【PAT刷题甲级】部分笔记1001-1064~(上)

写在前面 这里C菜鸟一枚~ 暑假将近两个月刷PAT甲级的笔记在此&#xff0c;虽然最终也没有取得满意的结果&#xff0c;但是不可否认&#xff0c;还是从中学到了很多&#xff0c;是我准备的还不够充分&#xff0c;希望能给刷题的同学们一些帮助。 PS&#xff1a;不是每道题都有&…

maya blend shape

参考&#xff1a;https://www.bilibili.com/video/BV1sJ411374p 建立两个模型&#xff0c;一个原始模型&#xff0c;一个表情模型的拓补关系要完全一样(点数面数啥的)两个模型在融合变形前需要清楚历史&#xff0c;否则会出问题雕刻模型可以直接在zbrush里雕&#xff0c;layer …

【PAT刷题甲级】部分笔记1065-1155~(下)

写在前面 接上篇【PAT刷题甲级】部分笔记1001-1064~&#xff08;上&#xff09; 这里是1065-1155&#xff0c;同样仅为本人刷的题部分笔记&#xff0c;不包括所有题&#xff0c;希望对刷题的同学有帮助~ 上篇&#xff1a;【PAT刷题甲级】部分笔记1001-1064~&#xff08;上&…