【PAT】1136 A Delayed Palindrome(大整数加法及其测试点分析) 避坑指南

31 篇文章 2 订阅
14 篇文章 0 订阅

Consider a positive integer N written in standard notation with k+1 digits a​i​​ as a​k​​ ⋯a​1​​ a​0​​ with 0≤a​i​​ <10 for all i and a​k​​ >0. Then N is palindromic if and only if a​i​​ =a​k−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.

**

PAT OJ入口

思想:利用一个int数组来存储大整数的每位字符,利用加法的原理对大整数的每位进行操作。代码中将各个功能都用一个函数来实现了,虽然不够精简但是十分利于大家学习。

**
大家可以参考我的另外一篇博客关于大整数比较,大整数的加法,大整数的减法。

大整数加减比较操作

我一共提交了三次出现了两次格式错误和一次逻辑错误,修改完后代码成功AC。

  1. 第一次因为+号和=号两边没有加空格,导致格式错误,这个错误特别傻逼啊,没错我是傻逼。修改后通过一个测试点。
  2. 第二次是因为输出 ‘is a palindromic number.’是is前面少了一个空格,确实我可能真的傻逼了吧。修改后拿了16分。
  3. 最后一次也是大家要注意的,一般对于你刚输入的数如果直接判断它是一个 delayed palindrome. 那么就直接输出,不需要再进行计算了。大家可以测试一下输入0。

AC代码如下

#include<bits/stdc++.h>
using namespace std;
struct bignum{
	int data[2000];
	int len;
	bignum(){
		memset(data,0,sizeof(data));
		len = 0;
	}
};
//string to bignum  字符串转换为大整数 
bignum convert(string str){
	bignum bn;
	int len_str = str.length();
	bn.len = len_str;
	for(int i=0;i<len_str;i++){
		bn.data[len_str-1-i] = str[i] -'0';
	}
	return bn;
}
//大整数加法 
bignum add(bignum a,bignum b){
	bignum c;
	int carry=0;
	for(int i=0;i<a.len||i<b.len;i++){
		int temp = a.data[i]+b.data[i]+carry;
		carry = temp/10;
		c.data[c.len++]=temp%10;
	}
	if(carry!=0) c.data[c.len++] = carry;
	return c;
}
//大整数打印
void print(bignum a){
	for(int i=a.len-1;i>=0;i--){
		printf("%d",a.data[i]);
	}
}
//大整数逆置,借助algorithm 中的reverse 
bignum reverse_bign(bignum num1){
	bignum num2 = num1;
	reverse(num2.data,num2.data+num2.len);
	return num2;
}
//判断大整数是否为 delayed palindrome  
int judge(bignum a){
	for(int i=0;i<a.len/2;i++){
		if(a.data[i] != a.data[a.len-1-i]) return 0;
	}
	return 1;
}
int main(){
#ifdef ONLINE_JUDGE
#else
	freopen("1.txt","r",stdin);//从1.txt读入 
#endif
	string str;
	cin>>str;
	bignum num1 = convert(str);
	//注意优先判断一次输入的数是否为 palindromic number 。 
	//!!!有测试点 
	if(judge(num1)){
		print(num1);cout<<" is a palindromic number.";
		return 0;
	}
	for(int i=0;i<10;i++){
		bignum num2 = reverse_bign(num1);
		bignum ans = add(num1,num2);
		//注意空格 
		print(num1);cout<<" + ";print(num2);cout<<" = ";print(ans);cout<<endl;
		if(judge(ans)!=0){
			print(ans);
			//注意空格 
			cout<<" is a palindromic number.";
			return 0;///直接退出程序不要执行循环外的语句了 
		}
		num1 = ans;
	}
	cout<<"Not found in 10 iterations.";
	return 0;
}

输入0(我是通过文件输入的)之后的运行结果应该为:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值