欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

PAT-A-1024 Palindromic Number 【大数相加】

程序员文章站 2022-07-13 17:55:29
...

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

我愿意把他称为大数相加

PS,int的有效表示是-2147483648到2147483647,简记为2开头的10位数。但是题目中没有保证所有的输入和输出结果都是int类型能表示的,因此首选使用字符串模拟大数相加。使用int或者long long的都会有一些测试用例过不去

#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;
bool isPalin(string a) {                         //比较是否相等,只需要比较到size/2位置即可
	int size = a.size();
	for (int i = 0; i <= size / 2; i++) {
		if (a[i] == a[size - i - 1]) continue;
		return false;
	}
	return true;
}
string getAdd(string s) {                   //string模拟大数相加
	int size = s.size() - 1;  
	int flag = 0;                           //flag进位标志,如果有进位置1,否者置0
	string rs = s;
	for (int i = size; i >= 0; i--) {
		int a = s[i] - '0';
		int b = rs[size - i] - '0';
		int sum = a + b + flag;
		if (sum >=10) {                     //一定要>=10
			sum -= 10;
			flag = 1;
		}
		else
		{
			flag = 0;
		}
		s[i] = sum + '0';
	}
	if (flag == 1) {                      //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
		s.insert(0, 1, '1');              //这里要注意,如果最后的进位等于1,说明结果size比
	}                                     //a的size大1,所以要在a前面插入1
	return s;                             //string.inset(pos,count,char);

}
int main() {
	string data;                           //既然没说int范围,干脆使用string保存
	int  m;
    cin>>data;
	scanf("%d",&m);
	for (int i = 0; i < m; i++) {
		if (isPalin(data)) {              
			printf("%s\n", data.c_str());
			printf("%d\n", i);
			return 0;
		}
		data = getAdd(data);
	}
	printf("%s\n", data.c_str());
	printf("%d\n", m);
	return 0;
}

 

PAT-A-1024 Palindromic Number 【大数相加】