题意:

给出一个数N(N<=10^10),最多可操作K次(K<=100),每次操作为这个数和其反转之后的数相加,若得到的结果为回文数,则输出;若在K次迭代后仍然不是回文数,在输出第K次操作后的结果。

思路:

因为int型最大可表示为2^31-1=2,147,483,647,显然本题的范围已经超出了int可表示的范围了。因此可用大整数的思想。
大整数相加时最后要注意一下进位carry是否大于0。
这里用到一点小trick,需要熟悉string的构造函数。这里使用了 ' 这样的写法表示的是int型整数"9"对应的char型。
 
代码:

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

bool judge(const string& str)
{
    ,j=str.size()-;
    while(i<j){
        if(str[i]!=str[j]) return false;
        i++;
        j--;
    }
    return true;
}

string add(const string& a,const string& b)
{
    string c;
    ;
    ;i>=;i--){
        ';
        c=,temp%+')+c;
        carry=temp/;
    }
    ) c=,carry+')+c;
    return c;
}

int main()
{
    ;
    string str,tmp;
    cin>>str>>k;
    while(cnt<=k){
        if(judge(str)){
            cout<<str<<'\n'<<cnt<<'\n';
            break;
        }
        if(cnt==k) tmp=str;
        string r_str=str;
        reverse(r_str.begin(),r_str.end());
        str=add(str,r_str);
        cnt++;
    }
    ) cout<<tmp<<'\n'<<k<<'\n';
    ;
}
 

1024 Palindromic Number的更多相关文章

  1. PAT 甲级 1024 Palindromic Number

    1024. Palindromic Number (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...

  2. PAT 甲级 1024 Palindromic Number (25 分)(大数加法,考虑这个数一开始是不是回文串)

    1024 Palindromic Number (25 分)   A number that will be the same when it is written forwards or backw ...

  3. 1024 Palindromic Number int_string转换 大整数相加

    A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...

  4. 1024. Palindromic Number (25)

    A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...

  5. PAT 1024 Palindromic Number[难]

    A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...

  6. PTA (Advanced Level) 1024 Palindromic Number

    Palindromic Number A number that will be the same when it is written forwards or backwards is known ...

  7. 1024 Palindromic Number (25)(25 point(s))

    problem A number that will be the same when it is written forwards or backwards is known as a Palind ...

  8. 【PAT】1024. Palindromic Number (25)

    A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...

  9. PAT Advanced 1024 Palindromic Number (25) [数学问题-⼤整数相加]

    题目 A number that will be the same when it is written forwards or backwards is known as a Palindromic ...

  10. 1024 Palindromic Number (25 分)

    A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...

随机推荐

  1. node-wechat 微信推送消息

    https://github.com/nswbmw/node-wechat/blob/master/index.js

  2. respond.js第六行 SCRIPT5: 拒绝访问。跨域问题

    问题描述:respond.js第六行 SCRIPT5: 拒绝访问.昨天为学弟学妹讲bootstrap,说到对ie78的兼容问题,解决办法中有引入html5shiv.js和respond.js两个文件夹 ...

  3. 【scala】集合框架

  4. 【2018年全国多校算法寒假训练营练习比赛(第四场)-A】石油采集(匈牙利算法)

    试题链接:https://www.nowcoder.com/acm/contest/76/A [思路] 每个‘#’的右边和下边如果也是‘#’说明这两个点构成通路,以此重构一幅图,然后找二分图的最大匹配 ...

  5. CSS:Tutorial one

    1.Three Ways to Insert CSS External style sheet Internal style sheet Inline style External Style She ...

  6. jsp中解决乱码问题

    解决中文乱码 a) 第一种: String name=new String(name.getBytes("ISO-8859-1"),"UTF-8"); b) 第 ...

  7. QT画图

    if (0) { QApplication a(argv, args); QGraphicsScene scene; scene.setSceneRect(-300,-300,600,600); sc ...

  8. 使用Metasploit收集邮箱信息

    Metasploit提供了很多辅助的模块,非常实用.今天介绍一个叫search_email_collector的模块,它的功能是查找搜索引擎(google.bing.yahoo),收集和某个域名有关的 ...

  9. [转载] FFMPEG视音频编解码零基础学习方法

    在CSDN上的这一段日子,接触到了很多同行业的人,尤其是使用FFMPEG进行视音频编解码的人,有的已经是有多年经验的“大神”,有的是刚开始学习的初学者.在和大家探讨的过程中,我忽然发现了一个问题:在“ ...

  10. NOI 2018 你的名字

    因为机房里的小伙伴都在看<你的名字.>而我不想看 所以来写了这道题... 给一个 $S$ 串,$q$ 次询问,每次一个 $T$ 串,问 $T$ 有多少没在 $S[l,r]$ 中以子串形式出 ...