1024 Palindromic Number(大整数)(回文数判断)

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 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

/**
Palindromic 回文
本题题意给定一个数n , 与一个k (限定相加次数), 判断是否是回文数, 如果不是, 则与原数的导致相加得到一个新的n, 判断
是否是回文数, 如果不是 继续相加此时n的倒置, 直到 到了规定的次数, 如果依然不是 则输出此时的n, 同时也要输出步骤。
遇到的问题:
        1.将变量的值写死 , 写成了测试用例了 (细心)
        2.将maxsize范围设定小了
本题思路:
    题中n 的范围是 10的 10次方, 因为还需要相加, 因此 使用大整数解题思路,
    因为是与原数的倒置相加,
        1.因此采用 大整数add,  
            a .相加时 可以只用一个 数组 首尾相加。
            b. 也可以 设定两个数组 (其中一个数组 是另一个数组的倒置 (可以采用reverse函数) 进行数组相加
        
    在判定回文数 时 可以单独分离出一个函数,只用比较相加后的数组, 首尾 结点是否相同, 因此只用遍历数组 leng / 2的 范围即
    可以判断
**/

 

具体代码实现:

#include<iostream>
#include<algorithm>
using namespace std;
const int maxsize = 1000; //此处范围设置过小 题目中虽然给定是10个数, 但是 数字最多可以加100此 因此范围设为 15显然会出错 
int k, steps = 0;
string n;
struct bign{
	int data[maxsize];
	int length;
	bign(){
		data[maxsize] = {0};
		length = 0;
	}
};
bign change(string s){
	bign c;
	for(int i = 0; i < s.size(); i++){
		c.data[c.length++] = s[s.size() - i - 1] - '0'; //此处一定要通过ascall码转换 
	}
	return c; 
}
//bign sub(bign a){  这个是直接用 一个数组实现 回文数的相加,  
//	bign c;
//	int carry = 0;
//	for(int i = 0; i < a.length; i++){
//		int temp = carry + a.data[i] + a.data[a.length - i - 1];
//		carry = temp / 10;
//		c.data[c.length++] = temp % 10;
//	}
//	if(carry != 0)
//		c.data[c.length++] = carry;
//	return c;
//}
bign sub(bign a, bign b){ //设定两个数组 进行回文数相加, 只不过第二数组需要反转以下 可以用reverse 函数, 或者自己写出反转函数 
	bign c;
	int carry = 0;
	for(int i = 0; i < a.length; i++){
		int temp = carry + a.data[i] + b.data[i];
		carry = temp / 10;
		c.data[c.length++] = temp % 10;
	}
	if(carry != 0)
		c.data[c.length++] = carry;
	return c;
}
void print(bign c){
	for(int i = 0 ; i < c.length; i++)
		printf("%d", c.data[c.length - i - 1]);
	printf("\n");
	printf("%d\n", steps);
}
bool judgePalin(bign c){ //判断是否是回文数, 无论n 是奇数或是偶数 n / 2 后, 起始处 与 终点处的结点相比较就能判断是否是回文数, 
	for(int i = 0; i < c.length / 2; i++){
		if(c.data[i] != c.data[c.length - i - 1])
			return false;
	}
	return true;
}
int main(){
	cin >> n >> k;
	bign a = change(n);
	while(!judgePalin(a) && steps < k){
			bign b = a;
			reverse(b.data, b.data + b.length);
			a = sub(a, b);
			steps++;
	} 
	print(a);
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值