日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【PAT - 甲级1024】Palindromic Number (25分)(大数,模拟)

發布時間:2023/12/10 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【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

題目大意:

?

解題報告:

沒仔細看,以為最多就100*1e10這么大。仔細一看需要上大數。寫了一個字符串版本的。

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 ; }

?

總結

以上是生活随笔為你收集整理的【PAT - 甲级1024】Palindromic Number (25分)(大数,模拟)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。