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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

【题意分析】1024 Palindromic Number (25 分)_38行代码AC

發布時間:2024/2/28 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【题意分析】1024 Palindromic Number (25 分)_38行代码AC 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

立志用最少的代碼做最高效的表達


PAT甲級最優題解——>傳送門


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


題意給定數N,次數K,將N翻轉后與原數相加,判斷是否為回文數,如果是則直接輸出,如果不是,則算作一次操作,操作次數上限為K。若K次后仍不是回文數,則直接輸出該數,結束循環。

分析利用了字符串函數翻轉機制縮減了代碼量。代碼邏輯很簡單,讀者可自行體會。


#include<bits/stdc++.h> using namespace std;bool is_palindormic(string s) { //判斷是否是回文數 string s1 =s;reverse(s.begin(), s.end());if(s1 == s) return true;else return false; }string ope (string s) { //執行相加操作 string s1 = s, add_s;reverse(s.begin(), s.end());int carry = 0, x;for(int i = 0; i < s.size(); i++) {x = (s[i]-'0' + s1[i]-'0' + carry) % 10;carry = (s[i]-'0' + s1[i]-'0' + carry) / 10;add_s += x+'0';}if(carry > 0) add_s += carry+'0';reverse(add_s.begin(), add_s.end());return add_s; }int main() {string s; int n, n1;cin >> s >> n;n1 = n;while(n1) if(is_palindormic(s)) { //如果是回文數,則輸出,結束循環 cout << s << '\n' << n-n1 << '\n'; goto loop;} else { //如果不是回文數, n1--;s = ope(s);}cout << s << '\n' << n << '\n'; loop : ;return 0; }

耗時:

超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生

總結

以上是生活随笔為你收集整理的【题意分析】1024 Palindromic Number (25 分)_38行代码AC的全部內容,希望文章能夠幫你解決所遇到的問題。

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