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

 

### C语言实现计算回文数个数 为了计算给定范围内的回文数个数,可以编写一个函数来判断单个整数是否为回文数,并遍历指定范围内所有的整数来进行统计。下面是一个完整的解决方案。 #### 判断单个整数是否为回文数 定义辅助函数 `isPalindrome` 来检测某个整数 n 是否构成回文结构: ```c #include <stdbool.h> bool isPalindrome(int num) { if (num < 0) return false; // 负数不是回文数 int originalNum = num; long reversedNum = 0; while (num != 0) { int digit = num % 10; reversedNum = reversedNum * 10 + digit; num /= 10; } return originalNum == reversedNum; } ``` 此部分代码实现了基本的反转数字并比较原数值与翻转后的值是否相等的功能[^5]。 #### 统计特定区间内所有回文数的数量 接下来创建另一个函数用于枚举从最小到最大之间的每一个可能候选者,并调用上述方法验证它们是不是满足条件;最后累加符合条件的结果数目返回即可: ```c int countPalindromesInRange(int minVal, int maxVal){ int palindromeCount = 0; for (int i=minVal ;i<=maxVal;i++){ if(isPalindrome(i)){ ++palindromeCount; } } return palindromeCount; } ``` 这段程序通过循环迭代的方式检查[minVal,maxVal]闭区间的每一个整数,利用之前定义好的`isPalindrome()`来做判定操作,从而得出总的回文数量。 #### 主函数示例 这里给出一段简单的测试代码作为示范用途,它会打印出1至100之间存在的全部正向负向都一样的自然数(即所谓的“回文数”)及其总数目: ```c #include <stdio.h> #include <stdlib.h> // ... 上面两个函数声明 ... int main(){ printf("Finding palindromic numbers between 1 and 100:\n"); int result = countPalindromesInRange(1, 100); printf("Total number of palindromic numbers found: %d\n",result); system("pause"); return 0; } ``` 该段脚本展示了如何组合前面提到过的各个组件形成最终的应用场景实例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值