【一只蒟蒻的刷题历程】 【PAT】 A1024 回文数

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


题目大意:

向前或向后写入时将相同的数字称为回文数。例如,1234321是回文数。所有一位数字都是回文数。

非回文数可以通过一系列操作与回文数配对。首先,将非回文数反转,并将结果添加到原始数中。如果结果不是回文数,则重复此操作直到给出回文数。例如,如果我们从67开始,我们可以分两步获得回文数:67 + 76 = 143,以及143 + 341 = 484。

给定任何正整数N,您应该找到它的配对回文数和找到它的步骤数。

输入规格:

每个输入文件包含一个测试用例。每种情况都由两个正数N和K组成,其中N(≤1010 10)是初始数字,K(≤100)是最大步数。这些数字用空格分隔。

输出规格:

对于每个测试用例,输出两个数字,每行一个。第一个数字是配对的回文数N,第二个数字是查找回文数所采取的步骤数。如果在K步之后未找到回文数,则只需输出在K步获得的数,然后输出K。

样本输入1:

67 3

样本输出1:

484
2

样本输入2:

69 3

样本输出2:

1353
3


思路:

第一次没看出来数很大,所以就普通模拟了一遍,结果只对了四个测试点,后来发现了数字很大,用了高精度加法,输入时n用int输入,然后to_string转字符串,最后两个测试点怎么都过不了。。。检查了好几个小时,后来直接把n定义为string就过了。。。


代码:

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
string add(string x) //逆序 并且相加
{
	string ans;
	string s = x;
	reverse(s.begin(),s.end()); //s为逆序后的数,x原来的数
	
	int carry=0;   //进位
	for(int i=s.size()-1;i>=0;i--) //高精度加法
	{
		int sum = s[i]-'0'+x[i]-'0'+carry;
		ans += sum%10+'0';
		carry = sum/10;
	}
	if(carry) ans+=carry+'0';
	reverse(ans.begin(),ans.end()); //最后还要倒置一遍
	return ans;
}

int main() 
{
   string s;
    /*刚开始定义成int,然后用to_string转成字符串,
    最后两个测试点一直过不了(现在也不知道为什么),
    不知道为什么,直接改成string就可以了*/
   int k;
   cin>>s>>k;
   string temp=s;
   reverse(temp.begin(),temp.end());
   //如果一开始本身就是回文,那就直接输出
   if(temp==s) cout<<s<<endl<<0; 
   else
   {
   	   int now =0 ;
   	   while(now<k) 
   	   {
   	     now++;
   	   	 s = add(s); //倒序并相加
   	     temp = s;
   	     reverse(temp.begin(),temp.end());
   	     if(temp == s) break; //if回文 结束循环
	   }
   	   cout<<s<<endl<<now;
   }
   
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章