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 ...
随机推荐
- 洛谷P4458 /loj#2512.[BJOI2018]链上二次求和(线段树)
题面 传送门(loj) 传送门(洛谷) 题解 我果然是人傻常数大的典型啊-- 题解在这儿 //minamoto #include<bits/stdc++.h> #define R regi ...
- ceph_osd故障检测
1. 当前monitor可以通过3种途径检测到osd离线 1) Osd自主上报 2) Osd通过投票的方式(满足一下条件之一,mon会将osd标记为down) a) ...
- jeecms学习笔记
jeecms学习笔记 内容管理系统 1.栏目列表标签 作用:获取栏目的列表 [@cms_channel_list] [#list tag_list as b] <a href="${b ...
- JDBC_时间处理_Date_Time_Timestamp区别_随机日期生成
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement;import ...
- [USACO10MAR]伟大的奶牛聚集 BZOJ 1827 树形dp+dfs
题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...
- elemetnui 分页..解决 bug
:current-page.sync="thisCurPage" 的确会 修改页码,但是会触发一个 bug... 比如 当前 选择的是第二页,使用 js 模拟 点击(或者修改父组件 ...
- 【算法笔记】B1038 统计同成绩学生
1038 统计同成绩学生 (20 分) 本题要求读入 N 名学生的成绩,将获得某一给定分数的学生人数输出. 输入格式: 输入在第 1 行给出不超过 105 的正整数 N,即学生总人数.随后一行给 ...
- 江西理工大学南昌校区排名赛 C: 单身狗的地图游戏
题目描述 萌樱花是一只单身狗. 萌樱花今天决定去喜欢的学妹家玩,但他不记得路,于是他拿出有n个点的完全图,选取了k条不同的路. 完全图:n 个点的图中任意两个点之间都有一条边相连,所以有 n*(n-1 ...
- HDU - 1085 母函数
年轻人的第一道母函数入门题 #include<bits/stdc++.h> using namespace std; const int maxn = 1000+2000+5000+1; ...
- B - Toy Storage(POJ - 2398) 计算几何基础题,比TOYS多了个线段排序
Mom and dad have a problem: their child, Reza, never puts his toys away when he is finished playing ...