【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 3Sample Output 1:
484 2Sample Input 2:
69 3Sample Output 2:
1353 3題目大意:
?
解題報(bào)告:
沒仔細(xì)看,以為最多就100*1e10這么大。仔細(xì)一看需要上大數(shù)。寫了一個(gè)字符串版本的。
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<stack> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define FF first #define SS second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 2e5 + 5; int k; string n; string add(string a,string b) {int p = a.length(),c=0;string res(p,'0');for(int i = p-1; i>=0; i--) {res[i] = c + (a[i]-'0') + (b[i]-'0') + '0';c = 0;if(res[i] > '9') c = 1,res[i] -= 10;}if(c > 0) res = "1" + res;return res; } bool is(string a) {int p = a.length();for(int i = 0; i<=p/2; i++) if(a[i] != a[p-i-1]) return 0;return 1; } int main() {cin>>n>>k;if(is(n)) {cout << n << endl << 0 << endl;return 0 ;}for(int i = 1; i<=k; i++) {string n2 = n;reverse(n2.begin(),n2.end());n = add(n,n2); if(is(n)) {cout << n << endl << i << endl;return 0;}}cout << n << endl << k << endl;return 0 ; }?
總結(jié)
以上是生活随笔為你收集整理的【PAT - 甲级1024】Palindromic Number (25分)(大数,模拟)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 20年老牌CPU厂商转型!RISC-V架
- 下一篇: 骁龙8+旗舰性价屠夫 曝realme G