当前位置:网站首页>【无标题】
【无标题】
2022-07-28 04:00:00 【烧烤大团子】
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 (≤1010) 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题意分析:
由于不断的翻转相加,最后变成了大整数运算,会溢出,所以转换为string字符串类型来计算,主要用到了reverse()函数来处理字符串的翻转,注意这个大整数相加函数add()的写法
代码如下:
#include<bits/stdc++.h>
using namespace std;
string s;
bool isPalindromic(string s)
{
string s2 = s;
reverse(s.begin(), s.end());
return s2==s;
}
void add(string t) {
int len = s.length(), carry = 0;
for (int i = 0; i < len; i++) {
s[i] = s[i] + t[i] + carry - '0';
carry = 0;
if (s[i] > '9') {
s[i] = s[i] - 10;
carry = 1;
}
}
if (carry) s += '1';
reverse(s.begin(), s.end());
}
int main()
{
int k;
cin >> s >> k;
int step = 0;
while (step <= k)
{
if (isPalindromic(s)) {
cout << s << "\n" << step << endl;
return 0;
}
if (step == k)break;
string right = s;
reverse(right.begin(), right.end());
add(right);
step++;
}
cout << s << "\n" << k << endl;
return 0;
}运行结果如下:

边栏推荐
- Do you regret doing automated testing?
- [wrong question]mocha and railgun
- Iterator function operation of iterator learning
- Experience sharing of automatic test for students with monthly salary of 28K
- Cookies and session
- Advanced Mathematics (Seventh Edition) Tongji University exercises 3-5 personal solutions
- 巧用栈回溯,帮你快速定位问题
- Selenium -- Web automated testing tool
- Screenshot of deepstream detection results
- [untitled]
猜你喜欢

Qt:qmessagebox message box, custom signal and slot

一名合格的软件测试工程师,应该具备哪些技术能力?

Embedded development: tips and techniques -- the best practice of defensive programming with C

Lightpicture - exquisite drawing bed system

numeric_ Limits the range and related attributes of each data type learned

Analysis of static broadcast transmission process

In depth introduction to sap ui5 fileuploader control - why do you need a hidden iframe trial

Regression - linear regression

Leetcode skimming: dynamic programming 08 (segmentation and subsets)

高等数学(第七版)同济大学 习题3-6 个人解答
随机推荐
Interface automation test, complete introduction
LeetCode_409_最长回文串
Istio's Traffic Management API
Leetcode 0140. word splitting II
C language: realize the exchange of two numbers without creating temporary variables
My creation anniversary
ServletContext、request、response
Which stock exchange has the lowest commission? Is it safe to open an account on your mobile phone
VBA reads the create document of SQL in batches to create tables
Dynamic planning - 62. Different paths
Recursion and non recursion are used to calculate the nth Fibonacci number respectively
【LeetCode】34、在排序数组中查找元素的第一个和最后一个位置
Move notice!
【无标题】
常用的弱网测试工具
超好用的 PC 端长截图工具
Do you regret doing automated testing?
Advanced Mathematics (Seventh Edition) Tongji University exercises 3-5 personal solutions
【无标题】
cookie与Session