PAT1024. Palindromic Number (25)

本文介绍了一段用于将输入字符串转换为回文的算法,并提供了判断字符串是否为回文的功能。通过逐步解析字符串并进行回文验证,实现对字符串的高效处理。

输入即为回文的情况要考虑

 1 #include <iostream>
 2 #include <algorithm> //reverse
 3 using namespace std;
 4 string n;
 5 int k;
 6 string strAdd(string a){
 7     string b;
 8     int num,cc=0;
 9     int len=a.size();
10     for(int i=0;i<len;i++){
11         num=a[i]+a[len-i-1]-2*'0'+cc;
12         cc=num/10;
13         b.push_back(num%10+'0');
14     }
15     if(cc!=0) b.push_back('1');
16     reverse(b.begin(),b.end());
17     return b;
18 }
19 bool isPali(string &a){
20     int len=a.size();
21     for(int i=0;i<len/2;i++){
22         if(a[i]==a[len-1-i]){continue;}
23         else{return false;}
24     }
25     return true;
26 }
27 int main()
28 {
29     cin>>n>>k;
30     int c=k;
31     string result=n;
32     if(isPali(result)) {cout<<result<<endl<<"0";return 0;}
33     while(c--){
34         result=strAdd(result);
35         if(isPali(result))break;
36     }
37     if(c<=0){
38         cout<<result<<endl;
39         cout<<k;
40     }else{
41         cout<<result<<endl;
42         cout<<(k-c);
43     }
44     return 0;
45 }

 

转载于:https://www.cnblogs.com/yellowman/p/5305740.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值