PAT甲级1024

PAT甲级1024Palindromic 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 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 (≤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 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

原题大意

给一个小于1e10的正整数,和一个小于100的执行次数。如果该数不是回文数,则将其倒置并与原数求和赋值给原数,如果更改后的数是回文数则输出回文数和重复这样计算的次数;如果在规定次数测试完后还不是回文数,则输出回文数和最大的次数。
规定输出格式:共两行:第一行输出计算的数,第二行输出得到回文数的次数。

解题思路

  首先得用一个数组存放一个整数的每位的数。由于初始的数最大9位,且每次计算加法时最多增加一位,故最多100次计算下来,最多增加100位,加上原来的9位,最终最多不超过110位。故数组分配110位足够!
  接下来写两个函数,一个用来判断该数是否为回文数,一个用来加法计算。

个人感觉需注意的点

  1. 刚开始给的数字也可能是回文数,记得判断;
  2. 用来存放位数的数组不能太短,不然会溢出;

小白分享,欢迎大佬不吝赐教!

源码

#include <iostream>
using namespace std;
//判断是否是回文数 
bool isPaN(int a[],int length){
	int temp=length-1,i;
	for(i=0;i<length/2;i++){
		if(a[i]!=a[temp-i])return false;
	}
	return true;
}
//数和倒置求和 
void Add(int a[],int& length){
	int temp[length],i;//临时数组,存放倒置的数组值 
	int add=0,index=length-1;//add是记录进位是否为1 
	for(i=0;i<length;i++)
		temp[i]=a[index-i];
	for(i=index;i>=0;i--){
		a[i]+=temp[i]+add;
		if(a[i]>=10){
			a[i]=a[i]-10;
			add=1;
		} 
		else add=0;
		//cout<<i<<':'<<a[i]<<endl;
	}
	if(add==1){//发生进位,长度+1 
		for(i=length;i>=1;i--)
			a[i]=a[i-1];
		a[0]=1; 
		length+=1;
	}
}
int main(){
	long int n;
	int k,i,length=0;
	cin>>n>>k;
	int a[110]={0};
	while(n>0){
		a[length++]=n%10;
		n=n/10;
	}
	//本身就是回文数 
	if(isPaN(a,length)){
		for(i=0;i<length;i++)
			cout<<a[i];
		cout<<endl;
		cout<<0;
		return 0;
	} 
	//一步步测试
	for(i=1;i<=k;i++){
		Add(a,length);
		if(isPaN(a,length)){
			k=i;
			break;
		}
	}
	for(i=0;i<length;i++)
		cout<<a[i];
	cout<<endl;
	cout<<k;
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值