题意:

给出一个数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. 分分享知识-快乐自己: @Component注解的使用

    @controller 控制器(注入服务) 用于标注控制层,相当于struts中的action层 @service 服务(注入dao) 用于标注服务层,主要用来进行业务的逻辑处理 @repositor ...

  2. css中zoom:1以及z-index的作用

    一.CSS中zoom:1的作用在做IE6.IE7.IE8浏览器兼容的时候,经常会遇到一些问题,可以使用zoom:1来解决,有如下作用:1.触发IE浏览器的haslayout2.解决IE下的浮动,mar ...

  3. 通过TortoiseSVN checkout的文件前面没有“状态标识”

    问题描述:安装完成VisualSVN Server.VisualSVn和TortoiseSVN后,然后通过SVN Server新建Repository(仓库),用Visual Studio新建一个So ...

  4. 简要谈谈javascript bind 方法

    最近去参加了场面试,跟面试官聊了很多JS基础上的东西,其中有个问题是谈谈对apply.call.bind的理解和区别.顿时一愣,apply.call我知道,经常用的东西,bind是什么鬼!!!好像见过 ...

  5. Django -- DRF 认证流程

    Django Restful Framework (DRF)中类的调用与自定义-- 以 autentication 认证为例 DRF 的 request 对 django 的 request 进行了更 ...

  6. Failed to export application

    打包Android项目,遇到Failed to export application的错误提示.如何处理呢 我当时是 在替换图标时   没有完全替换 只替换了 四张drawable_h图片,没有替换上 ...

  7. ng 双向数据绑定 实现 注册协议效果

    效果: 代码: <!DOCTYPE html> <html ng-app="myApp"> <head lang="en"> ...

  8. ng 监听数据的变化

    $scope.$watch('监听的变量的名称',func) 在angularJs之所以能够实现绑定,是因为angularJS框架在背后为每一个模型数据添加了一个监听,与$watch其实是一个道理. ...

  9. html中<video>显示视频的时候,MP4的格式问题

    html支持的视频格式:HTML5视频 注意 浏览器对mp4 的编码方式要求的非常严格 视频编码必须是H.264 音频编码必须是: AAC

  10. django初探-创建简单的博客系统(一)

    django第一步 1. django安装 pip install django print(django.get_version()) 查看django版本 2. 创建项目 打开cmd,进入指定目录 ...