首页 > 其他 > 详细

1079 延迟的回文数

时间:2020-02-25 16:40:27      阅读:94      评论:0      收藏:0      [点我收藏+]

注意点:如果输入的数是 回文数,那么无需计算,直接输出即可。

这题和 B1019数字黑洞 类似,用string处理 即可!

ps:第一次做时,写了68行,第二次做时,写了38行。。。

#include<iostream>
#include<algorithm>
using namespace std;

bool isPalindromicNumber(const string& str) {//判断是否是回文数
    int n = str.size();
    for(int i = 0; i < n/2; ++i)
        if(str[i] != str[n-1-i]) return false;
    return true;
}

int main() {
    string str;
    cin>>str;
    int i;
    for(i = 0; i < 10; ++i) {
        if(isPalindromicNumber(str)) {
            printf("%s is a palindromic number.",str.c_str());
            break;
        }
        string temp = str;
        reverse(temp.begin(),temp.end());
        printf("%s + %s = ",str.c_str(),temp.c_str());
        string result;
        int carry = 0;
        for(int i = str.size()-1; i >= 0; --i) {
            result += (str[i]-0+temp[i]-0+carry)%10 +0;
            carry = (str[i]-0+temp[i]-0+carry)/10;
        }
        if(carry != 0) result += carry +0;
        reverse(result.begin(),result.end());
        printf("%s\n",result.c_str());
        str = result;
    }
    if(i == 10)
        printf("Not found in 10 iterations.");
    return 0;
}

技术分享图片

 

1079 延迟的回文数

原文:https://www.cnblogs.com/keep23456/p/12362251.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!