cheng_you_know

学而时习之!

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <string.h>
#include <vector>
using namespace std;
char s1[200];
char s2[200];
char *reverse(char *s)
{
    if(s==NULL)
        return NULL;
    int end = strlen(s) -1;
    int begin = 0;
    char temp;
    while(begin <= end)
    {
        temp = s[begin];
        s[begin] = s[end];
        s[end] = temp;
        begin++;
        end --;
    }
    return s;
}

bool if_Palindromic(char *s)  //判断是否是回文数
{
    int end = strlen(s)-1;
    int begin = 0;
    while(begin <= end)
    {
        if(s[begin]!=s[end])
            return false;
        begin++;
        end --;
    }
    return true;
}


char *chars_add(char *a,char *b)
{
    int i,temp;
    int len = strlen(a);
    int carry = 0;
    for(i=len-1;i>=0;i--)
    {
        temp = (a[i]-'0')+(b[i]-'0') + carry;
        a[i]= temp%10 + '0';
        carry = temp/10;
    }
    if(carry > 0)  //末尾有进位,则整体往后移动一位,放置进位
    {
        for(i = len-1;i>=0;i--)
            a[i+1] = a[i];
        a[len+1] = 0;
        a[0] = carry + '0';
    }
    return a;
}
int main()
{
    int i,j,k,len1,temp,len2;
    int step;
    while(scanf("%s %d",s1,&k)!=EOF)
    {
        if(if_Palindromic(s1))
        {
            printf("%s\n0\n",s1);
            continue;
        }
        step = 0;
        while(step < k)
        {
            strcpy(s2,s1);
            reverse(s1);
            chars_add(s1,s2);
            step++;
            if(if_Palindromic(s1))
                break;
        }
        printf("%s\n%d\n",s1,step);
    }
    return 0;
}
posted on 2014-01-21 21:54  cheng_you_know  阅读(254)  评论(0编辑  收藏  举报