PAT 1024 Palindromic Number
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm> using namespace std; void print(vector<char> &num) {
int len = num.size();
bool value_begin = false;
for (int i=; i<len; i++) {
if (!value_begin && num[i] == ) continue; // skip leading zeros;
value_begin = true;
printf("%d", num[i]);
}
if (!value_begin) { // totally zero
printf("");
}
} vector<char> num2vec(long long num) {
vector<char> ret; while (num) {
ret.push_back(num % );
num /= ;
}
reverse(ret.begin(), ret.end());
return ret;
} vector<char> add(vector<char> &a, vector<char> &b) {
vector<char> ret;
int alen = a.size();
int blen = b.size();
int carry= ;
while (alen > && blen > ) {
int d = carry + a[--alen] + b[--blen];
carry = d / ;
d = d % ;
ret.push_back(d);
} while (alen > ) {
int d = carry + a[--alen];
carry = d / ;
d = d % ;
ret.push_back(d);
}
while (blen > ) {
int d = carry + b[--blen];
carry = d / ;
d = d % ;
ret.push_back(d);
}
if (carry) ret.push_back();
reverse(ret.begin(), ret.end());
return ret;
} vector<char> pal(vector<char> &num) {
vector<char> ret = num;
reverse(ret.begin(), ret.end());
return ret;
} bool is_pal(vector<char> &num) {
int len = num.size();
int p = , q = len - ;
while (p < q) {
if (num[p] != num[q]) break;
++p, --q;
}
return p >= q;
} int main() {
long long n = , k = , i = ;
scanf("%ld%ld", &n, &k);
vector<char> num = num2vec(n);
for (i=; i<k; i++) {
if (is_pal(num)) {
break;
}
vector<char> pnum = pal(num);
vector<char> tnum = add(pnum, num);
swap(num, tnum);
}
print(num);
printf("\n%d", i);
return ;
}
要注意数值范围,非常喜欢在这种地方搞你一下
PAT 1024 Palindromic Number的更多相关文章
- PAT 1024 Palindromic Number[难]
A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...
- PAT 甲级 1024 Palindromic Number
1024. Palindromic Number (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...
- 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 (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)(25 point(s))
problem A number that will be the same when it is written forwards or backwards is known as a Palind ...
- 1024 Palindromic Number int_string转换 大整数相加
A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...
- PAT A1024 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)
A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...
随机推荐
- 洛谷P1251 餐巾计划问题(费用流)
传送门 不得不说这题真是思路清奇,真是网络流的一道好题,完全没想到网络流的建图还可以这么建 我们把每一个点拆成两个点,分别表示白天和晚上,白天可以得到干净的餐巾(购买的,慢洗的,快洗的),晚上可以得到 ...
- [Swift]八大排序算法(三):选择排序 和 简单选择排序
排序分为内部排序和外部排序. 内部排序:是指待排序列完全存放在内存中所进行的排序过程,适合不太大的元素序列. 外部排序:指的是大文件的排序,即待排序的记录存储在外存储器上,待排序的文件无法一次装入内存 ...
- 【spring cloud】源码分析(一)
概述 从服务发现注解 @EnableDiscoveryClient入手,剖析整个服务发现与注册过程 一,spring-cloud-common包 针对服务发现,本jar包定义了 DiscoveryCl ...
- Python之路Python作用域、匿名函数、函数式编程、map函数、filter函数、reduce函数
Python之路Python作用域.匿名函数.函数式编程.map函数.filter函数.reduce函数 一.作用域 return 可以返回任意值例子 def test1(): print(" ...
- Jenkins项目部署使用教程-----01安装
基本配置: 1.Linux安装配置jdk环境 1.1.上传到 Linux 服务器:例如: 上传至: cd /usr/local 1.2.解压: rpm -ivh jdk-8u111-linux-x64 ...
- 二、为什么要选用pytest以及 pytest与unittest比较
为什么要选择pytest,我看中的如下: 写case,不需要像unittest那样,创建测试类,继承unittest.TestCase pytest中的fixture(类似于setUp.tearDow ...
- 微信小程序之页面之间传递值
页面之间传值有三种方式 1.url传值 2.本地存储传值 3.全局变量传值 1.url传值: 通过url传值的需要通过option来获取参数值. 更多详情可以访问小程序-navigateTo章节. A ...
- HDU6464 (权值线段树)-(查找区间第k1小于第k2小之间的和)
http://acm.hdu.edu.cn/showproblem.php?pid=6464 不理解先看博客:https://blog.csdn.net/g21glf/article/details/ ...
- [转] Spring Boot实战之Filter实现使用JWT进行接口认证
[From] http://blog.csdn.net/sun_t89/article/details/51923017 Spring Boot实战之Filter实现使用JWT进行接口认证 jwt(j ...
- 联想g400怎么进bios设置u盘启动图文教程
联想g400怎么进bios设置u盘启动图文教程 转自http://www.kqidong.com/bios/3940.html 虽然成功学会u盘装系统的人很多,但是设置u盘启动在小白们的眼中却没有那么 ...