大数据加法
给一个数num和最大迭代数k
每次num=num+num的倒序,判断此时的num是否是回文数字,是则输出此时的数字和迭代次数
如果k次结束还没找到回文数字,输出此时的数字和k

如果num一开始是回文数字,那么直接输出num和0即可。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <string>
using namespace std;
const int maxn=;
struct Bign{
int len,num[maxn]; // 这里num[0]是最低位,num[len-1]是最高位
Bign(){
memset(num,,sizeof(num));
len=;
}
Bign(int val){
*this=val;
}
Bign(const char*val){
*this=val;
}
//以int型赋值
Bign operator=(int val){
char s[maxn];
sprintf(s,"%d",val);
*this=s; //字符串赋值
return *this;
}
//以字符串赋值
Bign operator=(const char*val){
len=strlen(val);
for(int i=;i<len;i++){
num[i]=val[len--i]-'';
}
return *this;
}
//去掉前导0
void clean(){
while(len>&&!num[len-]){
len--;
}
}
//转化为字符串
string tostr(){
string res="";
clean();
for(int i=;i<len;i++){
res=(char)(num[i]+'')+res;
}
if(res=="")
return "";
return res;
}
//重载+
Bign operator+(const Bign& b)const{
Bign c;
c.len=;
for(int i=,g=;g||i<max(len,b.len);i++){
int x=g;
if(i<len)
x+=num[i];
if(i<b.len)
x+=b.num[i];
c.num[c.len++]=x%;
g=x/;
}
return c;
}
//取翻转后的数
Bign toReverse(){
clean();
Bign c;
c.len=len;
for(int i=;i<len;i++){
c.num[i]=num[len--i];
}
return c;
} };
//判断是否是回文数字
bool isPal(Bign s){
int l=,r=s.len-;
while(l<=r){
if(s.num[l]!=s.num[r])
return false;
l++;
r--;
}
return true;
}
int main()
{
char s[];
int k;
scanf("%s %d",s,&k);
Bign bign=s;
if(isPal(bign)){
printf("%s\n%d\n",s,);
}
else{
for(int i=;i<=k;i++){
//cout<<"bign:"<<bign.tostr()<<" bign Reverse:"<<bign.toReverse().tostr()<<endl;
bign=bign+bign.toReverse();
//cout<<" "<<bign.tostr()<<endl;
if(isPal(bign) || i==k){
string ans=bign.tostr();
cout<<ans<<endl<<i<<endl;
break;
}
} }
return ;
}

PAT甲题题解-1024. Palindromic Number (25)-大数运算的更多相关文章

  1. PAT甲题题解-1117. Eddington Number(25)-(大么个大水题~)

    如题,大水题...贴个代码完事,就这么任性~~ #include <iostream> #include <cstdio> #include <algorithm> ...

  2. PAT甲题题解-1051. Pop Sequence (25)-堆栈

    将1~n压入最多为m元素的栈 给出k个出栈序列,问你是否能够实现. 能输出YES 否则NO 模拟一遍即可,水题. #include <iostream> #include <cstd ...

  3. PAT甲题题解-1059. Prime Factors (25)-素数筛选法

    用素数筛选法即可. 范围long int,其实大小范围和int一样,一开始以为是指long long,想这就麻烦了该怎么弄. 而现在其实就是int的范围,那难度档次就不一样了,瞬间变成水题一枚,因为i ...

  4. PAT甲题题解-1101. Quick Sort (25)-大水题

    快速排序有一个特点,就是在排序过程中,我们会从序列找一个pivot,它前面的都小于它,它后面的都大于它.题目给你n个数的序列,让你找出适合这个序列的pivot有多少个并且输出来. 大水题,正循环和倒着 ...

  5. PAT甲题题解-1130. Infix Expression (25)-中序遍历

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789828.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  6. PAT甲题题解-1129. Recommendation System (25)-排序

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789819.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  7. PAT甲题题解-1016. Phone Bills (25)-模拟、排序

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789229.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  8. PAT甲题题解-1021. Deepest Root (25)-dfs+并查集

    dfs求最大层数并查集求连通个数 #include <iostream> #include <cstdio> #include <algorithm> #inclu ...

  9. PAT甲题题解-1028. List Sorting (25)-水排序

    #include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...

随机推荐

  1. 写博的第一天 0x00

    作为一个大学生,我觉得自己除了睡觉,打游戏,吃饭更应该做点有意义的事情,作为计算机专业的学生,更应该勤奋点,主动去学习.已经要大三了,我认识到这个暑假我必须学点什么了,为了更好的学习,我决定学语言的同 ...

  2. [python]通过uiautomator实现返回当前程序包名

    # -*- coding: utf-8 -*- from uiautomator import device as d def getCurrentPackageName(): info = d.in ...

  3. FIO_工具_专业

    一.FIO工具安装: 1.查看fio是否安装 [root@localhost /]#rpm –qa|grep fio 2.源码安装(推荐) 官网地址:http://freecode.com/proje ...

  4. 金三银四求职季,前端面试题小梳理(HTML、CSS、JS)

    好久没写学习记录,最近太多事,又到一年求职季,都说金三银四求职季,自己也做一下最近学习的一些前端面试题梳理,还是个小白,写的不对请指正,不胜感激. HTML篇 html语义化 用语义化的代码标签书写, ...

  5. Nowcoder 提高组练习赛-R1

    https://www.nowcoder.com/acm/contest/172#question 单人报名300元,五人合报免费,于是就和学弟同学学长们组了一个三世同堂的队伍,高一的学长wzhqwq ...

  6. FFT && NTT板子

    贴板子啦-- FFT板子:luogu P3803 [模板]多项式乘法(FFT) #include<cstdio> #include<iostream> #include< ...

  7. java字符串利用dom4j转 xml 且遍历

    1.因为转换的格式不是标准格式,所以有时候获得xml根目录后rootElement.attributes() 取不到想要的属性 所以需要通过迭代器来获取想要的值 public static void ...

  8. mysql用户创建与授权

    一. 创建用户 命令: CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 说明: username:你将创建的用户名 host:指定该用户 ...

  9. collate字段详细讲解

    collate可以在库级别上,表级别上,列级别上设计:意思就是“排列规则”;通常和charset结合使用: 例如: 建议使用utf8mb4而不是utf8,因为utf8最多支持3字节得长度,但是有些字符 ...

  10. Ubuntu安装 Alisql编译安装步骤:

    github地址: https://github.com/alibaba/AliSQL 一.直接git下载保存到本地: git clone https://github.com/alibaba/Ali ...