题意:

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

trick:

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

AAAAAccepted code:

  1. #define HAVE_STRUCT_TIMESPEC
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4. char num[];
  5. char s[];//这里用string的时候会遇到以下的小问题,查阅资料后发现直接对string原有长度以后的位置赋值不能增加它的长度,建议采用s+=" xxx",的形式可以增加string的长度。
  6. int k;
  7. int main(){
  8. cin>>s>>k;
  9. int tot=k;
  10. int flag=;
  11. int siz=strlen(s);
  12. while(k--){
  13. memset(num,,sizeof(num));
  14. int cnt=;
  15. for(int i=siz-;i>=;--i)
  16. num[++cnt]=s[i];
  17. flag=;
  18. for(int i=;i<=cnt/;++i)
  19. if(num[i]!=num[cnt-i+]){
  20. flag=;
  21. break;
  22. }
  23. if(!flag){
  24. flag=;
  25. break;
  26. }
  27. string y;
  28. int jinwei=;
  29. for(int i=cnt;i;--i){
  30. int tt=num[i]+s[i-]-''-''+jinwei;
  31. y[i]=tt%+'';
  32. jinwei=tt/;
  33. }
  34. if(jinwei)
  35. y[]='';
  36. if(y[]!=)
  37. for(int i=;i<=cnt;++i)
  38. s[i]=y[i];
  39. else
  40. for(int i=;i<=cnt;++i)
  41. s[i-]=y[i];
  42. if(s[cnt]!=)
  43. siz=cnt+;//这里用siz来更新s的长度会在使用string s的时候出现段错误
  44. else
  45. siz=cnt;//这里如果不用siz来更新s的长度会在使用string s的时候s.size()不更新,依然是输入s的时候的size(),不是很懂,在size()-1以后的位置给s赋值,不能更新它的size(),用siz更新也会出现段错误
  46. }
  47. for(int i=;i<siz;++i)
  48. cout<<s[i];
  49. cout<<endl;
  50. if(flag==)
  51. cout<<tot-k-;
  52. else
  53. cout<<tot;
  54. return ;
  55. }

【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. css常见问题汇总

    1. 如果我想显示两行文字第二行超出部分‘...’? 限制在一个块元素显示的文本的行数. -webkit-line-clamp 是一个 不规范的属性(unsupported WebKit proper ...

  2. iOS-image图片旋转方向

    https://blog.csdn.net/qq_36557133/article/details/85760469 最近在做项目的时候发现资源包内的图片的方向不对,但也不想让UI切一个新图,所以需要 ...

  3. 安卓基础(Navigation)

    今天学习了简单的Navigation:页面导航. 页面导航的简单例子: MainAcitivity: package com.example.navigation; import android.su ...

  4. netty笔记-:Channel与ChannelHandlerContext执行write方法的区别

      在netty中有我们一般有两种发送数据的方式,即使用ChannelHandlerContext或者Channel的write方法,这两种方法都能发送数据,那么其有什么区别呢.这儿引用netty文档 ...

  5. POJ - 1845 Sumdiv(分治)

    题意:求$A^{B}$的所有约数之和$mod\ 9901$ 思路:由结论有,一个数$n$进行质因数分解得到$n={p_{1}}^{c_{1}} * {p_{2}}^{c_{2}} *...* {p_{ ...

  6. codeforces Beautiful Numbers

    来源:http://codeforces.com/problemset/problem/1265/B   B. Beautiful Numbers   You are given a permutat ...

  7. CentOS7中JDK的安装和配置

    1.使用yum线上安装jdk 这里以jdk1.7为例进行示范,1.8同理 yum -y list java*                  #浏览线上所有jdk版本列表,列表太长了,会显示不全 y ...

  8. python requests.request 和session.request区别究竟在哪里

    import requests hd={"X-auth":"eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJzeXN0ZW0iLCJBUEkiOiIvdW ...

  9. ASP.NET Core搭建多层网站架构【10-使用JWT进行授权验证】

    2020/01/31, ASP.NET Core 3.1, VS2019, Microsoft.AspNetCore.Authentication.JwtBearer 3.1.1 摘要:基于ASP.N ...

  10. 微服务架构中的Redis

    了解如何将Redis与Spring Cloud和Spring Data一起使用以提供配置服务器,消息代理和数据库. Redis可以广泛用于微服务架构中.它可能是少数流行的软件解决方案之一,你的应用程序 ...