码迷,mamicode.com
首页 > 其他好文 > 详细

PTA(Advanced Level)1023.Palindromic Number

时间:2020-03-26 01:01:00      阅读:67      评论:0      收藏:0      [点我收藏+]

标签:rev   str   false   integer   and   个数   name   tps   mat   

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 (≤1010) 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

思路

  • 题目大意:一个数\(a\),就正着看和反着看构成的数字相加,经过\(k\)次迭代之后是否为回文数,又因为这个数\(a\)是大数,所以要写的是大数的加法
  • 考察的就是高精度的加法,而且这里还是比较简单的高精度加法,因为一个数正着看和倒着看一定是长度相等的
  • 注意:如果一个数本身就是回文数的话我们就直接输出了,不用去计算,因为没注意这个WA了两个点

代码

#include<bits/stdc++.h>
using namespace std;
struct bignumber
{
	int num[1000];
	int len;
	bignumber()
	{
		memset(num, 0, sizeof(num));
		len = 0;
	}
};
bool is_palindromic(bignumber a)
{
	for(int i=0;i<=a.len/2;i++)
		if(a.num[i] != a.num[a.len - i - 1])
			return false;
	return true;
}	//判断是否为回文
bignumber assignment(string s)
{
	bignumber a;
	for(int i=0;i<s.size();i++)
		a.num[i] = s[s.size() - i - 1] - ‘0‘;
	a.len = s.size();
	return a;
}	//字符串->大数,数值低位对应数组低位
bignumber get_reverse(bignumber a)
{
	bignumber b;
	for(int i=0;i<a.len;i++)
		b.num[i] = a.num[a.len - i - 1];
	b.len = a.len;
	return b;
}	//返回a的reverse组成的数
bignumber big_add(bignumber a, bignumber b)
{
	bignumber c;
	int carry = 0;
	for(int i=0;i<a.len;i++)
	{
		c.num[c.len] = a.num[i] + b.num[i] + carry;
		carry = c.num[c.len] / 10;
		c.num[c.len++] %= 10;
	}
	while(carry!=0)
	{
		c.num[c.len++] = carry % 10;
		carry /= 10;
	}
	return c;
}	//大数相加,此处a和b的长度一定是相等的
int main()
{
	bignumber a,b;
	string s;
	int k;
	cin >> s >> k;
	a = assignment(s);
	b = get_reverse(a);
	bignumber c;

    if(is_palindromic(a))
    {
        for(int i=a.len-1;i>=0;i--)	cout << a.num[i];
        cout << endl;
        cout << 0;
        return 0;
    }	//如果本身就是,就不用再做下面的运算了

	int step = 0;
	for(int i=1;i<=k;i++)
	{
		c = big_add(a,b);
		step++;
		if(is_palindromic(c))
			break;
        a = c;
        b = get_reverse(a);
	}
	for(int i=c.len-1;i>=0;i--)	cout << c.num[i];
	cout << endl;
	step <= k ? cout << step : cout << k;
	return 0;
}

引用

https://pintia.cn/problem-sets/994805342720868352/problems/994805476473028608

PTA(Advanced Level)1023.Palindromic Number

标签:rev   str   false   integer   and   个数   name   tps   mat   

原文地址:https://www.cnblogs.com/MartinLwx/p/12571701.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!