Palindromic Number(注意数字过大的情况)

该博客探讨了一个编程问题,涉及找到一个非回文数的配对回文数。通过一系列操作,如反转数字并相加,直到得到回文数。程序处理了数字位数过大可能导致的溢出问题,使用字符串存储并进行逐位操作。文章还提醒注意数字位数过大的情况,防止在计算过程中出现错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Palindromic Number

题目

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

答案

#include<iostream>
using namespace std;
int main()
{
	string num;
	int k,cnt=0;
	cin>>num>>k;
	while(k--)
	{
		int len=num.length(),flag=0;
		string tmp1="",tmp2="";
		for(int i=0;i<len/2;i++)//判断是否为回文数 
		{
			if(num[i]!=num[len-1-i])
			{
				flag=1;break;
			}
		}
		if(!flag) break;
		cnt++;
		for(int i=0;num[i];i++)//正序 
		{
			tmp1+=num[i];
		}
		for(int i=len-1;i>=0;i--)//逆序 
		{
			tmp2+=num[i];
		}
		int t1=0,t2=0;
		for(int i=len-1;i>=0;i--)//诸位求和,保留进位 
		{
			t1=tmp1[i]-'0'+tmp2[i]-'0'+t2;
			num[i]=t1%10+'0';
			t2=t1/10;
		}
		if(t2!=0) num=to_string(t2)+num;
	}
	cout<<num<<endl;
	cout<<cnt;
}

注意

和这篇文章类似——Have Fun with Numbers(注意数字位数过大的问题),都是需要注意位数过大的情况

在计算机中,如果数字的位数超限,就会将其转为负数。为了避免这种情况,我们采用字符串存贮,相加时诸位相加、保留进位

def is_prime(n): """判断素数的函数,接收一个正整数为参数,参数是素数时返回True,否则返回False。减小判定区间,减少循环次数,提升效率""" #======================Begin================================= # 补充你的代码 #=========================End============================== def plalindrome_prime(number): """接收一个正整数参数number,遍历从0到number之间的所有整数, 若某个数是素数,且转为字符串后是回文字符串,则称其中回文素数 找出并在同一行中输出小于number的所有回文素数,每个数字后一个空格,函数无返回值。""" #======================Begin================================= # 补充你的代码 #=========================End============================== positive_int = int(input()) plalindrome_prime(positive_int) 任务描述 本关任务:编写一个能寻找回文素数的小程序。 相关知识 为了完成本关任务,你需要掌握: 寻找回文素数 寻找回文素数 如果一个整数是素数,同时其对应的字符串是回文字符串时,便称其为回文素数。例如,131 既是素数,其对应的字符串131又是回文字符串,所以 131 是回文素数。 输入一个正整数 n , 请你在一行内输出从小到排列的小于这个数的所有回文素数,每个数字后面一个空格。 编程要求 根据提示,在右侧编辑器补充代码,完善寻找回文素数的小程序。 测试说明 平台会对你编写的代码进行测试: 输入格式 输入一个正整数 输出格式 一行内输出从小到排列的小于这个数的所有回文素数,每个数字后面一个空格。 测试输入: 191 预期输出: 2 3 5 7 11 101 131 151 181 开始你的任务吧,祝你成功!
最新发布
05-17
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值