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 ...
随机推荐
- nginx负载均衡fair模块安装和配置
nginx-upstream-fair-master fair模块源码 官方github下载地址:https://github.com/gnosek/nginx-upstream-fair说明:如果从 ...
- 在请求中存取属性setAttribute&getAttribute方法
在请求中保存属性: public void setAttribute(String name,Object o) request.setAttribute("mess"," ...
- Tarjan 点双+割点+DFS【洛谷P3225】 [HNOI2012]矿场搭建
P3225 [HNOI2012]矿场搭建 题目描述 煤矿工地可以看成是由隧道连接挖煤点组成的无向图.为安全起见,希望在工地发生事故时所有挖煤点的工人都能有一条出路逃到救援出口处.于是矿主决定在某些挖煤 ...
- Linux crontab定时任务命令使用记录
安装crontab 使用 crontab -v 如果提示没有该命令,则需要安装.安装也很简单,推荐使用yum安装.一条命令即可(yum install crontab),这里不多介绍. 下面是一些基础 ...
- AForge.net 录像拍照功能实现 转
AForge.net 使用之录像拍照功能实现 最近使用aforge.NET拍照录像功能实现 记录一下以便以后好学习,哈哈,直接上代码 连接摄像头设备,这里需要引入 AForge.Video; AFor ...
- 洛谷 P1579 哥德巴赫猜想(升级版)
嗯... 这或许也算一道数论题吧... 题目链接:https://www.luogu.org/problemnew/show/P1579 这道题的说明好像只会扰乱人的思路....然后就是这道题的细节比 ...
- 转载---<html>与<body>
关于根元素html以及body的对比,主要是遇到设置背景色的问题,这里转载张鑫旭的关于html和body对比的文.(直接贴过来,是为了以后自己方便看) 原文地址:http://www.zhangxin ...
- Linux环境查看系统参数
一.查看CPU信息 lscpu cat /proc/cpuinfo 二.查看CPU位数 getconf LONG_BIT 三.查看MEM信息 free free -m cat /proc/me ...
- tar命令加密压缩/解密解压
在tar解压文件时发生下面错误信息 gzip: stdin: not in gzip format tar: Child returned status 1 tar: Error is not rec ...
- I/O设备
I/O(Input/Output)设备是指与计算机进行数据传输的硬件,具体分为字符设备.块设备.网络设备. 字符设备 字符设备(character device),又叫做人机交互设备.用户通过这些设备 ...