题意:

输入两个正整数N和K(N<=1e10,k<=100),求K次内N和N的反置相加能否得到一个回文数,输出这个数和最小的操作次数。

trick:

1e10的数字相加100次可能达到1e40,所以long long会爆,采用字符数组操作,以及代码注释中遇到的一些小问题。

AAAAAccepted code:

 #define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
char num[];
char s[];//这里用string的时候会遇到以下的小问题,查阅资料后发现直接对string原有长度以后的位置赋值不能增加它的长度,建议采用s+=" xxx",的形式可以增加string的长度。
int k;
int main(){
cin>>s>>k;
int tot=k;
int flag=;
int siz=strlen(s);
while(k--){
memset(num,,sizeof(num));
int cnt=;
for(int i=siz-;i>=;--i)
num[++cnt]=s[i];
flag=;
for(int i=;i<=cnt/;++i)
if(num[i]!=num[cnt-i+]){
flag=;
break;
}
if(!flag){
flag=;
break;
}
string y;
int jinwei=;
for(int i=cnt;i;--i){
int tt=num[i]+s[i-]-''-''+jinwei;
y[i]=tt%+'';
jinwei=tt/;
}
if(jinwei)
y[]='';
if(y[]!=)
for(int i=;i<=cnt;++i)
s[i]=y[i];
else
for(int i=;i<=cnt;++i)
s[i-]=y[i];
if(s[cnt]!=)
siz=cnt+;//这里用siz来更新s的长度会在使用string s的时候出现段错误
else
siz=cnt;//这里如果不用siz来更新s的长度会在使用string s的时候s.size()不更新,依然是输入s的时候的size(),不是很懂,在size()-1以后的位置给s赋值,不能更新它的size(),用siz更新也会出现段错误
}
for(int i=;i<siz;++i)
cout<<s[i];
cout<<endl;
if(flag==)
cout<<tot-k-;
else
cout<<tot;
return ;
}

【PAT甲级】1024 Palindromic Number (25 分)的更多相关文章

  1. PAT 甲级 1024 Palindromic Number (25 分)(大数加法,考虑这个数一开始是不是回文串)

    1024 Palindromic Number (25 分)   A number that will be the same when it is written forwards or backw ...

  2. PAT 甲级 1024 Palindromic Number

    1024. Palindromic Number (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...

  3. 【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 ...

  4. 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 ...

  5. 1024 Palindromic Number (25 分)

    A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...

  6. 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 ...

  7. PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习

    1020 Tree Traversals (25分)   Suppose that all the keys in a binary tree are distinct positive intege ...

  8. PAT 甲级 1146 Topological Order (25 分)(拓扑较简单,保存入度数和出度的节点即可)

    1146 Topological Order (25 分)   This is a problem given in the Graduate Entrance Exam in 2018: Which ...

  9. PAT 甲级 1071 Speech Patterns (25 分)(map)

    1071 Speech Patterns (25 分)   People often have a preference among synonyms of the same word. For ex ...

随机推荐

  1. 单选按钮 设置required属性无法进行非空验证

    先看代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  2. 8.5-Day1T3--Asm.Def 的一秒

    题目大意 略... (吐槽这题面...让我毫无阅读兴趣) 题解 首先要求出在以两条斜线为新坐标轴下,每个点的坐标 那么....按x先排序 再求y的最长上升子序列 复杂度O(nlogn)吧 记得开lon ...

  3. 事件和方法的区别,以input框的blur事件为例

    1. 我们在原生的js中学到的事件 onblur 2. 使input框失去焦点的方法blur 3. jquery中的方法blur 是当input框失去焦点时触发的回调 三者是不相同的 事件:指的是一个 ...

  4. VS常用快捷键(2012)

    Ctrl+K,D ----格式化全部代码 Ctrl+K,F ----格式化选中的代码 Ctrl+K,C ----注释选定内容 Ctrl+K,U ----取消注释选定内容 Ctrl+J或者 Ctrl+S ...

  5. SPOJ Distinct Substrings

    给定一个字符串,求不相同子串个数.每个子串一定是某个后缀的前缀,那么原问题等价于求所有后缀之间的不相同子串个数.总数为n*(n-1)/2,再减掉height[i]的和就是答案 #include< ...

  6. Bug搬运工-CSCvn51483:AP datasheet should contain info if the AP contains or not temperature sensor

    目前在老一些的思科AP上,应该都没有温度传感器的功能(例如AP1600,wave 1 AP1700...),但是在思科官方没有明确的文档去说明这个问题. 在Outdoor AP(室外AP,例如AP15 ...

  7. 洛谷 P1880 [NOI1995]石子合并(区间DP)

    嗯... 题目链接:https://www.luogu.org/problem/P1880 这道题特点在于石子是一个环,所以让a[i+n] = a[i](两倍长度)即可解决环的问题,然后注意求区间最小 ...

  8. ios 用touchend事件 pc用click touchend击穿

    var clickEvent = (function() { if ('ontouchend' in document.documentElement === true) return 'touche ...

  9. try catch和if else

    当错误发生时,当事情出问题时,JavaScript 引擎通常会停止,并生成一个错误消息.描述这种情况的技术术语是:JavaScript 将抛出一个错误. try 语句允许我们定义在执行时进行错误测试的 ...

  10. U2000解决备份:服务器不可达、FTP/TFTP/SFTP IP地址与网管地址不一致

    只需要将一下几点设置到位这些问题基本解决: 设置->网元软件管理->FTP设置 如果你的U2000服务器和OLT都在一个内网,那么就IP1和IP2就都输入U2000服务器的内网IP,否则按 ...