题意:

给出一个数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. Java_WebKit

    1. http://tieba.baidu.com/p/2807579276 下载地址: http://qtjambi.org/downloads https://qt.gitorious.org/q ...

  2. BZOJ 4445 [Scoi2015]小凸想跑步:半平面交

    传送门 题意 小凸晚上喜欢到操场跑步,今天他跑完两圈之后,他玩起了这样一个游戏. 操场是个凸 $ n $ 边形,$ n $ 个顶点 $ P_i $ 按照逆时针从 $ 0 $ 至 $ n-1 $ 编号. ...

  3. iscroll.js的简单使用方法(总结)

    iscroll.js的简单使用方法(总结) 一.总结 一句话总结:Scroll是一个类,每个需要使用滚动功能的区域均要进行初始化. 最佳的HTML结构如下: <div id="wrap ...

  4. web自动化中的page object模式

    一. 原理 将页面的元素定位和元素行为封装成一个page类,实现页面对象和测试用例分离 类的属性:元素定位 类的行为:元素的操作 测试用例:调用所需页面对象中的行为,组成测试用例 二. 好处 1. 当 ...

  5. hdu 5265

    http://acm.hdu.edu.cn/showproblem.php?pid=5256 题目不错,题面忍不住骂一句mmp.......后面说ai都是正整数,我以为修改后也必须是正整数,前面又说只 ...

  6. (http请求+cookie)的交互流程

    如果步骤5携带的是过期的cookie或者是错误的cookie,那么将认证失败,返回至要求身份认证页面. 当前 Cookie 有两个版本:Version 0 和 Version 1.通过它们有两种设置响 ...

  7. puppet笔记

    简介: puppet是一种Linux.Unix平台的集中配置管理系统,使用ruby语言,可管理配置文件.用户.cron任务.软件包.系统服务等.puppet把这些系统实体称之为资源,puppet的设计 ...

  8. C#中的字符串及其编码转换

    (转自:http://blog.sina.com.cn/s/blog_498eab7d0100et7j.html) 根据查找的System.Text.Encoding类的属性,方法写了如下的转换程序: ...

  9. Struts04---命名空间的查询顺序以及默认执行的Action

    01.创建login.jsp <%@ page language="java" import="java.util.*" pageEncoding=&qu ...

  10. Android中Application是什么?

    Application是什么? Application和Activity,Service一样,是android框架的一个系统组件,当android程序启动时系统会创建一个 application对象, ...