zoukankan      html  css  js  c++  java
  • 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
    0
    ",s1);
                continue;
            }
            step = 0;
            while(step < k)
            {
                strcpy(s2,s1);
                reverse(s1);
                chars_add(s1,s2);
                step++;
                if(if_Palindromic(s1))
                    break;
            }
            printf("%s
    %d
    ",s1,step);
        }
        return 0;
    }
  • 相关阅读:
    第十一周学习总结
    个人冲刺——(六)
    第二阶段冲刺—第二天
    软件工程第十四周总结
    第二阶段冲刺—第一天
    大道至简阅读笔记02
    输入法用户体验评价
    软件工程第十三周总结
    人机交互-水王
    大道至简阅读笔记01
  • 原文地址:https://www.cnblogs.com/cheng07045406/p/3528924.html
Copyright © 2011-2022 走看看