[PAT A1024]Palindromic Number
题目描述
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.
输入格式
Each input file contains one test case. Each case consists of two positive numbers N and K, where N (≤1010) is the initial numer and K (≤100) is the maximum number of steps. The numbers are separated by a space.
输出格式
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.
输入样例1
67 3
输出样例1
484
2
输入样例2
69 3
输出样例2
1353
3
解析
- 题目的大意就是输入一个数N和最多操作次数K,不断地将它与它的转置相加,直到得到一个回文数或者达到最多操作次数K为止。例如题目给的样例:67 3,67+76=143,143+341=484,这里的得到回文数了,虽然最大次数是3,但是我们不用继续做下去了,输出484 2就行。至于69 3,经过三次运算后还没有得到一个回文数,这是我们输出经过K次运算后他的结果和K
- 这道题目不是很难,考察的依旧是大整数的运算,主要需要注意的就是,如果一个数本来就是回文数,那么我们就应该输出这个数并输出次数0
- 还有一点就是,为什么要使用大整数加法?因为这里的N是<1010的,而K是小于100的,我们不难发现,经过一次操作,我们得到的数要么跟原来的数同样位数,要么比原来位数还要多1,那么经过100次运算后,这个数至多可以达到100位,这使用整数可是存储不了的。
#include<iostream>
#include<string>
using namespace std;
struct bign
{
int bit[110];
int len;
bign() {
len = 0;
}
};
int main()
{
bign num, ans;
bool judge = true;
int maxs, s = 0;
string str;
cin >> str >> maxs;
int j = 0;
for (int i = str.length() - 1; i >= 0; i--) {
num.bit[j++] = str[i] - '0';
num.len++;
}
judge = true;
for (int i = 0, j = num.len - 1; i < j; i++, j--) {
if (num.bit[i] != num.bit[j]) judge = false;
}
if (judge) {
for (int i = num.len - 1; i >= 0; i--) printf("%d", num.bit[i]);
printf("\n0");
} //首先判断是否为回文数,是的话就不用进行下面的运算了,这里应该把判断回文数写成一个函数的,但是我一开始没有写,所以也懒得改了
else {
for (int i = 0; i < maxs; i++) {
int carry = 0;
for (int i = 0; i < num.len; i++) {
int j = num.len - 1 - i;
ans.bit[i] = num.bit[i] + num.bit[j] + carry;
if (ans.bit[i] >= 10) {
ans.bit[i] -= 10;
carry = 1;
}
else carry = 0;
}
if (carry == 1) {
ans.len = num.len + 1;
ans.bit[ans.len - 1] = 1;
}
else ans.len = num.len; //计算加之后的结果
s++; //运算次数+1
judge = true;
for (int i = 0, j = ans.len - 1; i < j; i++, j--) {
if (ans.bit[i] != ans.bit[j]) judge = false;
}//判断回文数
if (judge) {
for (int i = ans.len - 1; i >= 0; i--) printf("%d", ans.bit[i]);
printf("\n%d", s);
break;
}
else num = ans; //如果不是回文数,那么我们把ans赋值给num继续运算
}
}
if (!judge) {
for (int i = ans.len - 1; i >= 0; i--) printf("%d", ans.bit[i]);
printf("\n%d", maxs);
}
return 0;
}