标签:rev str false integer and 个数 name tps mat
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.
67 3
484
2
69 3
1353
3
#include<bits/stdc++.h>
using namespace std;
struct bignumber
{
int num[1000];
int len;
bignumber()
{
memset(num, 0, sizeof(num));
len = 0;
}
};
bool is_palindromic(bignumber a)
{
for(int i=0;i<=a.len/2;i++)
if(a.num[i] != a.num[a.len - i - 1])
return false;
return true;
} //判断是否为回文
bignumber assignment(string s)
{
bignumber a;
for(int i=0;i<s.size();i++)
a.num[i] = s[s.size() - i - 1] - ‘0‘;
a.len = s.size();
return a;
} //字符串->大数,数值低位对应数组低位
bignumber get_reverse(bignumber a)
{
bignumber b;
for(int i=0;i<a.len;i++)
b.num[i] = a.num[a.len - i - 1];
b.len = a.len;
return b;
} //返回a的reverse组成的数
bignumber big_add(bignumber a, bignumber b)
{
bignumber c;
int carry = 0;
for(int i=0;i<a.len;i++)
{
c.num[c.len] = a.num[i] + b.num[i] + carry;
carry = c.num[c.len] / 10;
c.num[c.len++] %= 10;
}
while(carry!=0)
{
c.num[c.len++] = carry % 10;
carry /= 10;
}
return c;
} //大数相加,此处a和b的长度一定是相等的
int main()
{
bignumber a,b;
string s;
int k;
cin >> s >> k;
a = assignment(s);
b = get_reverse(a);
bignumber c;
if(is_palindromic(a))
{
for(int i=a.len-1;i>=0;i--) cout << a.num[i];
cout << endl;
cout << 0;
return 0;
} //如果本身就是,就不用再做下面的运算了
int step = 0;
for(int i=1;i<=k;i++)
{
c = big_add(a,b);
step++;
if(is_palindromic(c))
break;
a = c;
b = get_reverse(a);
}
for(int i=c.len-1;i>=0;i--) cout << c.num[i];
cout << endl;
step <= k ? cout << step : cout << k;
return 0;
}
https://pintia.cn/problem-sets/994805342720868352/problems/994805476473028608
PTA(Advanced Level)1023.Palindromic Number
标签:rev str false integer and 个数 name tps mat
原文地址:https://www.cnblogs.com/MartinLwx/p/12571701.html