• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            付翔的專欄
            在鄙視中成長 記錄成長的點滴
            posts - 106,  comments - 32,  trackbacks - 0

            Reverse and Add



            The  reverse and add  function starts with a number, reverses its digits and adds the reverse to the original. If the sum is not a palindrome (meaning it does not give the same number read from left to right and right to left), we repeat this procedure until it does.

            For example, if we start with 195 as the initial number, we get 9,339 as the resulting palindrome after the fourth addition:

            195
            786
            1,473
            5,214
            591
            687
            3,741
            4,125
            + ---
            + ---
            + ---
            + ---
            786
            1,473
            5,214
            9,339

            This method leads to palindromes in a few steps for almost all of the integers. But there are interesting exceptions. 196 is the first number for which no palindrome has been found. It has never been proven, however, that no such palindrome exists.

            You must write a program that takes a given number and gives the resulting palindrome (if one exists) and the number of iterations/additions it took to find it.

            You may assume that all the numbers used as test data will terminate in an answer with less than 1,000 iterations (additions), and yield a palindrome that is not greater than 4,294,967,295.

            Input

            The first line will contain an integer N ( 0 < N$ \le$100), giving the number of test cases, while the next N lines each contain a single integer P whose palindrome you are to compute.

            Output

            For each of the N integers, print a line giving the minimum number of iterations to find the palindrome, a single space, and then the resulting palindrome itself.

            Sample Input

            3
            195
            265
            750

            Sample Output

            4 9339
            5 45254
            3 6666
            一個簡單的處理問題 直接用加法模板 過的 這個服務器由于是gcc標準 所有很多以前用習慣的函數 都不支持 如 strrev  還得自己寫個
            不過還好 不難

            貼上代碼
            #include <stdio.h>
            #include 
            <string.h>
            #include 
            <stdlib.h>
            const int maxn = 110;

            void add(char a[],char b[],char back[])
            {
                
            int i,j,k,up,x,y,z,l;
                
            char *c;
                
            if (strlen(a)>strlen(b)) l=strlen(a)+2else l=strlen(b)+2;
                c
            =(char *) malloc(l*sizeof(char));
                i
            =strlen(a)-1;
                j
            =strlen(b)-1;
                k
            =0;up=0;
                
            while(i>=0||j>=0)
                    {
                        
            if(i<0) x='0'else x=a[i];
                        
            if(j<0) y='0'else y=b[j];
                        z
            =x-'0'+y-'0';
                        
            if(up) z+=1;
                        
            if(z>9) {up=1;z%=10;} else up=0;
                        c[k
            ++]=z+'0';
                        i
            --;j--;
                }
                
            if(up) c[k++]='1';
                i
            =0;
                c[k]
            ='\0';
                
            for(k-=1;k>=0;k--)
                    back[i
            ++]=c[k];
                back[i]
            ='\0';
            }
            char *strrev(char *s)
            {
                
            if (s == NULL || s[0== '\0')
                    
            return s;    
                
            for (char t, *= s, *= s + strlen(s) - 1; p < q; p++, q--)
                    t 
            = *p, *= *q, *= t;
                
                
            return s;
            }
            bool isReverse(char str[])
            {
                
            char *temp;
                temp 
            = (char*)malloc(sizeof(char)*(strlen(str) +3));
                strcpy(temp,str);
                strrev(temp);
                
            //temp = strrev(str);
                if(strcmp(str,temp)==0)
                    
            return true;
                
            return false;
            }
            int main()
            {
                
            char str1[maxn],str2[maxn],back[maxn];
                
            int i,n,num,T;
                scanf(
            "%d\n",&T);
                
            while(T--)
                {
                    i 
            = 0;
                    gets(str1);
                    strcpy(str2,str1);
                    strrev(str2);
                    strcpy(back,
            "0");
                    
            while(!isReverse(str1))
                    {
                        add(str1,str2,back);
                        strcpy(str1,back);
                        strcpy(str2,str1);
                        strrev(str2);
                        i 
            ++;
                    }
                    printf(
            "%d %s\n",i,str1);

                }
                
                
            return 0;
            }


            posted on 2010-07-23 19:43 付翔 閱讀(285) 評論(0)  編輯 收藏 引用 所屬分類: ACM 數據結構

            <2010年7月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            常用鏈接

            留言簿(2)

            隨筆分類

            隨筆檔案

            文章分類

            文章檔案

            CSDN - 我的blog地址

            博客

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            久久人人爽人爽人人爽av| 久久久噜噜噜久久熟女AA片| 久久狠狠高潮亚洲精品| 精品久久香蕉国产线看观看亚洲 | 国产精品乱码久久久久久软件| 久久国产亚洲精品| 久久香蕉一级毛片| 日产精品久久久久久久| 国产精品久久久久9999高清| 精品久久久久久无码中文野结衣| 久久久久99这里有精品10| 久久精品国产清高在天天线| 久久午夜无码鲁丝片午夜精品| 无遮挡粉嫩小泬久久久久久久| 草草久久久无码国产专区| 漂亮人妻被黑人久久精品| 亚洲国产成人久久一区久久| 久久精品国产91久久综合麻豆自制| 亚洲国产精品综合久久一线| 久久久久四虎国产精品| 蜜臀av性久久久久蜜臀aⅴ | 久久久久99精品成人片三人毛片| 欧美色综合久久久久久| 国内精品伊人久久久久777| 国内精品久久久久影院一蜜桃| 久久av高潮av无码av喷吹| 久久精品aⅴ无码中文字字幕重口 久久精品a亚洲国产v高清不卡 | 久久电影网一区| 少妇人妻88久久中文字幕| 久久精品视频91| 国产精品伊人久久伊人电影| 91久久婷婷国产综合精品青草| 99久久夜色精品国产网站| 99久久做夜夜爱天天做精品| 一本一道久久a久久精品综合| 久久人人爽人人精品视频| 精品国产乱码久久久久久浪潮| 91精品国产综合久久四虎久久无码一级| 色综合久久久久综合体桃花网| 久久天天躁夜夜躁狠狠躁2022| 日韩欧美亚洲综合久久|