【PAT甲级】1024 Palindromic Number (25 分)
题意:
输入两个正整数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 分)的更多相关文章
- PAT 甲级 1024 Palindromic Number (25 分)(大数加法,考虑这个数一开始是不是回文串)
1024 Palindromic Number (25 分) A number that will be the same when it is written forwards or backw ...
- PAT 甲级 1024 Palindromic Number
1024. Palindromic Number (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...
- 【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 ...
- 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 ...
- 1024 Palindromic Number (25 分)
A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...
- 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 ...
- PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习
1020 Tree Traversals (25分) Suppose that all the keys in a binary tree are distinct positive intege ...
- PAT 甲级 1146 Topological Order (25 分)(拓扑较简单,保存入度数和出度的节点即可)
1146 Topological Order (25 分) This is a problem given in the Graduate Entrance Exam in 2018: Which ...
- PAT 甲级 1071 Speech Patterns (25 分)(map)
1071 Speech Patterns (25 分) People often have a preference among synonyms of the same word. For ex ...
随机推荐
- css常见问题汇总
1. 如果我想显示两行文字第二行超出部分‘...’? 限制在一个块元素显示的文本的行数. -webkit-line-clamp 是一个 不规范的属性(unsupported WebKit proper ...
- iOS-image图片旋转方向
https://blog.csdn.net/qq_36557133/article/details/85760469 最近在做项目的时候发现资源包内的图片的方向不对,但也不想让UI切一个新图,所以需要 ...
- 安卓基础(Navigation)
今天学习了简单的Navigation:页面导航. 页面导航的简单例子: MainAcitivity: package com.example.navigation; import android.su ...
- netty笔记-:Channel与ChannelHandlerContext执行write方法的区别
在netty中有我们一般有两种发送数据的方式,即使用ChannelHandlerContext或者Channel的write方法,这两种方法都能发送数据,那么其有什么区别呢.这儿引用netty文档 ...
- POJ - 1845 Sumdiv(分治)
题意:求$A^{B}$的所有约数之和$mod\ 9901$ 思路:由结论有,一个数$n$进行质因数分解得到$n={p_{1}}^{c_{1}} * {p_{2}}^{c_{2}} *...* {p_{ ...
- codeforces Beautiful Numbers
来源:http://codeforces.com/problemset/problem/1265/B B. Beautiful Numbers You are given a permutat ...
- CentOS7中JDK的安装和配置
1.使用yum线上安装jdk 这里以jdk1.7为例进行示范,1.8同理 yum -y list java* #浏览线上所有jdk版本列表,列表太长了,会显示不全 y ...
- python requests.request 和session.request区别究竟在哪里
import requests hd={"X-auth":"eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJzeXN0ZW0iLCJBUEkiOiIvdW ...
- ASP.NET Core搭建多层网站架构【10-使用JWT进行授权验证】
2020/01/31, ASP.NET Core 3.1, VS2019, Microsoft.AspNetCore.Authentication.JwtBearer 3.1.1 摘要:基于ASP.N ...
- 微服务架构中的Redis
了解如何将Redis与Spring Cloud和Spring Data一起使用以提供配置服务器,消息代理和数据库. Redis可以广泛用于微服务架构中.它可能是少数流行的软件解决方案之一,你的应用程序 ...