码迷,mamicode.com
首页 > 其他好文 > 详细

PAT 1024 Palindromic Number

时间:2014-10-31 20:37:03      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   os   ar   for   sp   div   

#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>

using namespace std;

void print(vector<char> &num) {
    int len = num.size();
    bool value_begin = false;
    for (int i=0; i<len; i++) {
        if (!value_begin && num[i] == 0) continue; // skip leading zeros;
        value_begin = true;
        printf("%d", num[i]);
    }
    if (!value_begin) {         // totally zero
        printf("0");
    }
}

vector<char> num2vec(long long num) {
    vector<char> ret;

    while (num) {
        ret.push_back(num % 10);
        num /= 10;
    }
    reverse(ret.begin(), ret.end());
    return ret;
}

vector<char> add(vector<char> &a, vector<char> &b) {
    vector<char> ret;
    int alen = a.size();
    int blen = b.size();
    int carry= 0;
    while (alen > 0 && blen > 0) {
        int d = carry + a[--alen] + b[--blen];
        carry = d / 10;
        d = d % 10;
        ret.push_back(d);
    }
    
    while (alen > 0) {
        int d = carry + a[--alen];
        carry = d / 10;
        d = d % 10;
        ret.push_back(d);
    }
    while (blen > 0) {
        int d = carry + b[--blen];
        carry = d / 10;
        d = d % 10;
        ret.push_back(d);
    }
    if (carry) ret.push_back(1);
    reverse(ret.begin(), ret.end());
    return ret;
}

vector<char> pal(vector<char> &num) {
    vector<char> ret = num;
    reverse(ret.begin(), ret.end());
    return ret;
}

bool is_pal(vector<char> &num) {
    int len = num.size();
    int p = 0, q = len - 1;
    while (p < q) {
        if (num[p] != num[q]) break;
        ++p, --q;
    }
    return p >= q;
}

int main() {
    long long n = 0, k = 0, i = 0;
    scanf("%ld%ld", &n, &k);
    vector<char> num = num2vec(n);
    for (i=0; i<k; i++) {
        if (is_pal(num)) {
            break;
        }
        vector<char> pnum = pal(num);
        vector<char> tnum = add(pnum, num);
        swap(num, tnum);
    }
    print(num);
    printf("\n%d", i);
    return 0;
}

要注意数值范围,非常喜欢在这种地方搞你一下

PAT 1024 Palindromic Number

标签:style   blog   io   color   os   ar   for   sp   div   

原文地址:http://www.cnblogs.com/lailailai/p/4065886.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!