题意:

给出一个数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. DPDK之(八)——vhost库

    转:http://www.cnblogs.com/danxi/p/6652725.html vhost库实现了一个用户空间的virtio net server,允许用户直接处理virtio ring队 ...

  2. docker定义数据卷及数据卷的备份恢复

    前言:生产环境中使用docker时,往往需要对数据进行持久化(只有把容器导出为镜像,才能够保存写的数据,否则容器删除或者停止,所有数据都会没有),或者需要在多个容器之间进行数据共享,这必然涉及容器的数 ...

  3. GO学习笔记:函数作为值、类型

    在Go中函数也是一种变量,我们可以通过type来定义它,它的类型就是所有拥有相同的参数,相同的返回值的一种类型: type typeName func(input1 inputType1 , inpu ...

  4. H5测试

    H5是什么? H5的全称是HTML5,其实就是:移动端WEB页面. H5与原生 APP的区别: APP是使用原生系统内核的,相当于直接在系统上操作,是我们传统意义上的软件,更加稳定. H5的APP先得 ...

  5. HDU 3037 组合数、lucas,逆元

    题目链接 题目大意,N颗树上取不超过M个果子,求总方案个数模P的值,P是质数且不超过10w,N,M不超过1e9: 在这里树是被认为不同的,也就是将k(0<=k<=M)个小球放入N个不同的盒 ...

  6. pxcook-高效易用的自动标注工具, 生成前端代码

    1.pxcook.sketch(http://www.fancynode.com.cn/pxcook)

  7. cylance做的机器学习相关材料汇总

    https://www.cylance.com/en_us/products/our-products/protect----threatzero.html 产品介绍 关键!!!! https://w ...

  8. (转)程序员应该知道的10个eclipse调试技巧

    调试不仅可以查找到应用程序缺陷所在,还可以解决缺陷.对于Java程序员来说,他们不仅要学会如何在Eclipse里面开发像样的程序,更需要学会如何调试程序.本文介绍了Java程序员必知的10个调试技巧, ...

  9. LeetCode OJ:Multiply Strings (字符串乘法)

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  10. request.setAttribute("username", username);//一定要保存,OGNL才能获取${username}

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, ...