PTA(Advanced Level)1023.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 (≤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

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

时间: 2024-11-07 20:06:23

PTA(Advanced Level)1023.Palindromic Number的相关文章

PTA (Advanced Level)1023 Have Fun with Numbers

Have Fun with Numbers Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, with no duplication. Double it we will obtain 246913578, which happens to be another 9-digit number consisting exactly the numbers

PTA(Advanced Level)1036.Boys vs Girls

This time you are asked to tell the difference between the lowest grade of all the male students and the highest grade of all the female students. Input Specification: Each input file contains one test case. Each case contains a positive integer N, f

PAT Advanced 1019 General Palindromic Number (20分)

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. Although palindromic numbers are most often cons

PTA(Advanced Level)1025.PAT Ranking

To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C - C Programming Language, M - Mathematics (Calculus or Linear Algrbra), and E - English. At the mean time, we encourage students by e

PTA (Advanced Level)1066 Root of AVL Tree

Root of AVL Tree An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. F

PTA (Advanced Level)1018 Public Bike Management

Public Bike Management There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the city. The Public Bike Manag

PTA (Advanced Level)1022 Digital Library

Digital Library A Digital Library contains millions of books, stored according to their titles, authors, key words of their abstracts, publishers, and published years. Each book is assigned an unique 7-digit number as its ID. Given any query from a r

PTA(Advanced Level)1011.World Cup Betting

With the 2010 FIFA World Cup running, football fans the world over were becoming increasingly excited as the best players from the best teams doing battles for the World Cup trophy in South Africa. Similarly, football betting fans were putting their

PTA(Advanced Level)1015.Reversible Primes

A reversible prime in any number system is a prime whose "reverse" in that number system is also a prime. For example in the decimal system 73 is a reversible prime because its reverse 37 is also a prime. Now given any two positive integers N (&