PAT(大数)——1024. Palindromic Number (25)

A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit numbers are palindromic numbers.

Non-palindromic numbers can be paired with palindromic ones via a series of operations. First, the non-palindromic number is reversed and the result is added to the original number. If the result is not a palindromic number, this is repeated until it gives a palindromic number. For example, if we start from 67, we can obtain a palindromic number in 2 steps: 67 + 76 = 143, and 143 + 341 = 484.

Given any positive integer N, you are supposed to find its paired palindromic number and the number of steps taken to find it.

Input Specification:

Each input file contains one test case. Each case consists of two positive numbers N and K, where N (<= 10^10) is the initial numer and K (<= 100) is the maximum number of steps. The numbers are separated by a space.

Output Specification:

For each test case, output two numbers, one in each line. The first number is the paired palindromic number of N, and the second number is the number of steps taken to find the palindromic number. If the palindromic number is not found after K steps, just output the number obtained at the Kth step and K instead.

Sample Input 1:
67 3
Sample Output 1:
484
2
Sample Input 2:
69 3
Sample Output 2:
1353
3

题目大意:

大整数相加+模拟。

题目解析:

这里用vector保存数据,模拟大数相加。

具体代码:

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
vector<int> add(vector<int> &a,vector<int> &b){
	vector<int> res;
	int flag=0;
	for(int i=0;i<a.size();i++){
		int tmp=a[i]+b[i]+flag;
		if(tmp>=10)
			flag=1;
		else
			flag=0;
		res.push_back(tmp%10);
	}
	if(flag==1)
		res.push_back(1);
	return res;
}
bool judge(vector<int> &a){
	for(int i=0;i<a.size()/2;i++)
		if(a[i]!=a[a.size()-1-i])
			return false;
	return true;
}
void print(vector<int> &a){
	for(int i=a.size()-1;i>=0;i--)
		cout<<a[i];
}
int main()
{
    string s;
    int k;
    vector<int> pre,post;
    cin>>s>>k;
    for(int i=s.size()-1;i>=0;i--)
    	pre.push_back(s[i]-'0');
	for(int i=0;i<=k;i++){
		if(judge(pre)==true||i==k){
			print(pre);
			cout<<endl<<i;
			return 0;
		}
		post=pre;
		reverse(post.begin(),post.end());
		pre=add(pre,post);
	}
    return 0;
}
全部评论

相关推荐

挥毫自在:想白嫖你呢
点赞 评论 收藏
分享
09-22 22:22
中山大学 Java
乌鱼子萨奇:羡慕你啊,直接转正了,都不用经历秋招的炼狱,但是你少经历了很多痛苦的事情啊
点赞 评论 收藏
分享
头像
11-03 16:48
已编辑
百度_高级研发工程师
事实是检验真理的唯一标准。&nbsp;无论我们怎么去说,去讲述,去证明,都抵不过一个offer来得实在,无论我们怎么去复现求职中的摸爬滚打、扒皮抽筋、狼狈不堪,都抵不过你在简历写上大厂的名字(外包不算)。&nbsp;所以在我求职期间,我什么话都不说,什么话都不讲,因为没有意义,虽然我总讲过程才是意义,但只有当你上岸的那一刻,你才有资格回想在水里的挣扎,只有等你出了山,你才知道山的全貌。&nbsp;我为什么一定要离开华为OD,难道它不稳定吗,不能赚钱吗。为了证明自己,那肯定有的。其实更多的是印证我的认知是否真的正确。&nbsp;(给不了解我的人交代一下背景,在下双非一本,gap一年,华为OD外包,摸爬滚打4个月,艰难上岸百度正编)一、...
先锋战士:说得很真诚。鄙视链自古有之,学历,家庭背景,财富,权利。从小有之,小学羡慕那些当班委的,中学羡慕那些学生会的,高中羡慕尖子班拿教学金的,大学羡慕高绩点,毕业了羡慕进大厂的。工作了,又羡慕高职级的,再后来又羡慕别人早早结婚的。我想表达的观点很简单,无论是华为od还是百度,都是经历,没有孰高孰低,为了抵达下一个风景,总会付出更多东西,但不就是人生吗?正如登山,每个阶段的山,都要想办法攀登,在博主的文字中,见到了坚持和积极寻找问题解决办法的心态
学历对求职的影响
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务