zju pat 1024 Palindromic Number

zju pat 1024 Palindromic Number
#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;
}
zju pat 1024 Palindromic Number

zju pat 1024 Palindromic Number

上一篇:正则表达式学习


下一篇:mac下开启和关闭ftp服务