前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PAT (Advanced Level) Practice 1024 Palindromic Number (25 分)

PAT (Advanced Level) Practice 1024 Palindromic Number (25 分)

作者头像
glm233
发布2020-09-28 10:53:14
3450
发布2020-09-28 10:53:14
举报

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:

代码语言:javascript
复制
67 3

Sample Output 1:

代码语言:javascript
复制
484
2

Sample Input 2:

代码语言:javascript
复制
69 3

Sample Output 2:

代码语言:javascript
复制
1353
3

思路:

定义一种操作:将某个数和它的反转后得到的数相加,并且称这是一步,不断执行直到得到数字为回文数为止;给定数字n和k步,问 是否能在k步内得到,能则输出最后的回文数和所需步数,否则则输出k步后的结果;

一开始我就犯了个错误,想当然地认为1e10再怎么操作是不可能爆longlong的,后来想想如果极端数据大概是1e10的规模,取相反数相加有可能每一次操作位数就增加1,那么最多一百次操作这样最大可达10^110次,总之绝对爆longlong,需要引入大数加法

其实如果没有大数加法就是和乙级水题一个水平了~

话休絮烦,上代码~

代码语言:javascript
复制
// luogu-judger-enable-o2
#include<bits/stdc++.h>
#include<unordered_set>
#define rg register ll
#define inf 2147483647
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
#define ll long long
#define maxn 100005
#define lb(x) (x&(-x))
const double eps = 1e-6;
using namespace std;
inline ll read()
{
 char ch = getchar(); ll s = 0, w = 1;
 while (ch < 48 || ch>57) { if (ch == '-')w = -1; ch = getchar(); }
 while (ch >= 48 && ch <= 57) { s = (s << 1) + (s << 3) + (ch ^ 48); ch = getchar(); }
 return s * w;
}
inline void write(ll x)
{
 if (x < 0)putchar('-'), x = -x;
 if (x > 9)write(x / 10);
 putchar(x % 10 + 48);
}
inline bool check(string s)
{
    for(rg i=0;s[i];i++)
    {
        if(s[i]!=s[s.length()-i-1])return false;
    }
    return true;
}
inline string add(string &a,string b)
{
    //cout<<a<<" "<<b<<endl;
    ll val=0;
    for(rg i=a.length()-1;i>=0;i--)
    {
        ll tep=a[i],tepp=b[i];
        a[i]=(a[i]-48+b[i]-48+val)%10+48;
        val=(tep+tepp+val-96)/10;
        //cout<<a[i]<<endl;
        
    }
    string tep;
    if(val)
    {
        tep=val+48;a=tep+a;
    }
    return a;
}
string s;
ll k,cnt;
int main()
{
    cin>>s>>k;
    while(!check(s)&&cnt<k)
    {
        string tep=s;
        reverse(tep.begin(),tep.end());
        s=add(s,tep);
        //cout<<s<<" "<<check(s)<<endl;
        cnt++;
    }
    cout<<s<<endl<<cnt<<endl;
    //while(1)getchar();
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-11-22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Input Specification:
  • Output Specification:
  • Sample Input 1:
  • Sample Output 1:
  • Sample Input 2:
  • Sample Output 2:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档