题意:

输入两个正整数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. 《gPRC使用protobuf构建微服务》阅读笔记

    首先我需要去了解一些概念,根据百度百科了解到: l  微服务架构:微服务架构是一项在云中部署应用和服务的新技术.微服务可以在“自己的程序”中运行,并通过“轻量级设备与HTTP型API进行沟通”. l  ...

  2. SQL语句 分组 多行合并成一行

    ,,'')) FROM Table d GROUP by Id 另外: sql 单引号转义:两个单引号转义为一个单引号 set @sql='STUFF((SELECT '','' + Names FR ...

  3. 国际化支持、activity生命周期、屏幕翻转的ui适配

    国际化 对于手机的不同语言做出不同的语言描述,这里只是简单的提一下,实际上针对比较大型的项目,有可能不同的语言要做不同的ui适配. 例如下面:中文长度不长但是在德语中占据很长的位置,这个就要针对性的适 ...

  4. gRPC Learning Notes

    简介 更多内容参考:https://www.grpc.io/docs/guides/ gRPC 是一个高性能.开源和通用的 RPC 框架,面向移动和 HTTP/2 设计.目前提供 C.Java 和 G ...

  5. collections模块、时间模块、random模块、os模块、sys模块、序列化模块、subprocess模块

    一.collections模块 1.其他数据类型 在内置数据类型(str.dict.list.tuple.set)的基础上,collections模块还提供了了几个额外的数据类型:Counter.de ...

  6. 页面布局 Wrap 组件 和 RaisedButton按钮

    一.RaisedButton 定义一个按钮 Flutter 中通过 RaisedButton 定义一个按钮.RaisedButton 里面有很多的参数,只讲简单的进行使用. return Raised ...

  7. C语言函数不能返回数组,但可以返回结构体

    为什么C语言函数可以返回结构体,却不可以返回数组?有这样的问题并不奇怪,因为C语言数组和结构体本质上都是管理一块内存,那为何编译器要区别对待二者呢? C语言函数为什么不能返回数组? 在C语言程序开发中 ...

  8. 实现简单HttpServer案例

    <html> <head> <title>第一个表单</title> </head> <body> <pre> me ...

  9. 吴裕雄--天生自然Numpy库学习笔记:NumPy 高级索引

    import numpy as np x = np.array([[1, 2], [3, 4], [5, 6]]) y = x[[0,1,2], [0,1,0]] print (y) import n ...

  10. MySQL数据库--基础简述

    MySQL数据库--基础简述 1.15.1 MySQL简介 Mysql是最流行的RDBMS(Relational Database Management System:关系数据库管理系统),特别是在W ...