PAT甲题题解-1024. Palindromic Number (25)-大数运算
大数据加法
给一个数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)-大数运算的更多相关文章
- PAT甲题题解-1117. Eddington Number(25)-(大么个大水题~)
如题,大水题...贴个代码完事,就这么任性~~ #include <iostream> #include <cstdio> #include <algorithm> ...
- PAT甲题题解-1051. Pop Sequence (25)-堆栈
将1~n压入最多为m元素的栈 给出k个出栈序列,问你是否能够实现. 能输出YES 否则NO 模拟一遍即可,水题. #include <iostream> #include <cstd ...
- PAT甲题题解-1059. Prime Factors (25)-素数筛选法
用素数筛选法即可. 范围long int,其实大小范围和int一样,一开始以为是指long long,想这就麻烦了该怎么弄. 而现在其实就是int的范围,那难度档次就不一样了,瞬间变成水题一枚,因为i ...
- PAT甲题题解-1101. Quick Sort (25)-大水题
快速排序有一个特点,就是在排序过程中,我们会从序列找一个pivot,它前面的都小于它,它后面的都大于它.题目给你n个数的序列,让你找出适合这个序列的pivot有多少个并且输出来. 大水题,正循环和倒着 ...
- PAT甲题题解-1130. Infix Expression (25)-中序遍历
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789828.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT甲题题解-1129. Recommendation System (25)-排序
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789819.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT甲题题解-1016. Phone Bills (25)-模拟、排序
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789229.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT甲题题解-1021. Deepest Root (25)-dfs+并查集
dfs求最大层数并查集求连通个数 #include <iostream> #include <cstdio> #include <algorithm> #inclu ...
- PAT甲题题解-1028. List Sorting (25)-水排序
#include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...
随机推荐
- November 09th, 2017 Week 45th Thursday
If we did all the things we are capable of, we would literally astound ourselves. 我们如果尽全力去完成我们能做到的事情 ...
- 极限编程核心价值:简单(Simplicity)
写在前面 在编写 ASP.NET Core 项目时,深感项目设计的无力感,在软件设计方面我还有很长的路要走.我一直以来都把代码当作一种艺术的存在,认为自己是个"艺术家",其实就是个 ...
- laravel的Eloquent中的get()和Query/Builder中的get()
Eloquent 中的get实际上是Eloquent/Builder中的get,得到的结果是个Collection对象,再调用Collection的first才得到collection中的一项,即一个 ...
- C++构造析构函数生命期及对象生命期
- C借函数指针构造映射
这是候老师的<深入浅出 MFC>中C借函数指针构造映射截图,可以看到MFC们的映射思想:
- Spring 事务回滚代码
在事务中实行的方法:org.springframework.transaction.interceptor.TransactionAspectSupport#invokeWithinTransacti ...
- 【转】网段,子网掩码,网络标识,IP划分
网段指一个计算机网络中使用同一物理层设备(传输介质,中继器,集线器等)直接通讯的那一部分.就是从一个IP到另一个IP 好比 从192.168.0.1到192.168.255.255这之间就是一个网段 ...
- 把php session 会话保存到redis
php的session会话默认时以文件形式保存在php.ini配置文件设置的会话缓存目录,文件保存会话的效率很低,每当每个用户登录一次就会在服务器上生成一个唯一的session_id文件,当用户登录量 ...
- 魔法少女【动态规划问题】——NYOJ1204
个人博客页:https://www.scriptboy.cn/202.html 题目描述: 前些时间虚渊玄的巨献小圆着实火了一把. 在黑长直(小炎)往上爬楼去对抗魔女之夜时,她遇到了一个问题想请你帮忙 ...
- java util.Date 转换为sql.Date
public static java.sql.Timestamp StrTransSqlDate(String date) { SimpleDateFormat simpleDateFormat = ...