PAT A1024 Palindromic Number (25)----字符串操作注意

Conclusion:

1.进位不仅仅加在两个数相加的时候,计算当前位向上进位也要考虑上一位的进位!!!!

代码:

#include<iostream>
#include<string>
using namespace std;
bool isRT(string s)
{
    for (int i = 0, j = s.length() - 1; i < j; i++, j--)
    {
        if (s[i] != s[j]){
            return false;
        }
    }
    return true;
}
string operationss(string ss)
{
    string fan = ss;
    string result=ss;
    int k = ss.length();
    for (int i = k - 1; i >= 0; i--)
        fan[k-i-1] = ss[i];
//    cout << "逆转字符串为:" << fan << endl;
    int cnt = 0;
    for (int i = k - 1; i >= 0; i--)
    {
        char p = (cnt + ss[i] + fan[i] - '0' - '0') % 10 + '0';
        result[i] = p;
    //    cout << "字符p等于:" <<p<< endl;
        cnt = (ss[i] + fan[i] - '0' - '0'+cnt) / 10;
    }
    if (cnt != 0){ string stt(1, '0' + cnt); result = stt+result; }
    //cout << "结果字符串为:" << result << endl;
    return result;
}
int main()
{
    string a;
    int n;
    cin >> a >> n;
    int step = 0; int flag = 0;
    while (step <n)
    {
        if (isRT(a)){ cout << a << endl; cout << step; flag = 1; break; }
        else{
            a=operationss(a);
            step++;
        }
    }
    if (flag != 1 && step==n){ cout << a << endl; cout << n; }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值