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

PAT1024. Palindromic Number

时间:2015-02-21 18:51:20      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:

A number that will be the same when it is written forwards or backwards is known as a Palindromic Number.  For example, 1234321 is a palindromic number.  All single digit numbers are palindromic numbers.

Non-palindromic numbers can be paired with palindromic ones via a series of operations. First, the non-palindromic number is reversed and the result is added to the original number. If the result is not a palindromic number, this is repeated until it gives a palindromic number.  For example, if we start from 67, we can obtain a palindromic number in 2 steps: 67 + 76 = 143, and 143 + 341 = 484.

Given any positive integer N, you are supposed to find its paired palindromic number and the number of steps taken to find it.

Input Specification:

Each input file contains one test case. Each case consists of two positive numbers N and K, where N (<= 1010) is the initial numer and K (<= 100) is the maximum number of steps.  The numbers are separated by a space.

Output Specification:

For each test case, output two numbers, one in each line.  The first number is the paired palindromic number of N, and the second number is the number of steps taken to find the palindromic number.  If the palindromic number is not found after K steps, just output the number obtained at the Kth step and K instead.

Sample Input 1:

67 3

Sample Output 1:

484
2

Sample Input 2:

69 3

Sample Output 2:

1353
3

思路:反转数字,然后大数相加。so easy
技术分享
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 struct Bignum
 6 {
 7     int d[100];
 8     int len;
 9     Bignum()
10     {
11         memset(d,0,sizeof(d));
12         len=0;        
13     }    
14 };
15 //判断是否是回文 
16 bool Judge(Bignum A)
17 {
18     for(int i=0;i<A.len/2;i++)
19     {
20         if(A.d[i]!=A.d[A.len-i-1])
21           return false;
22     } 
23     return true;
24 }
25 Bignum Reverse(Bignum A)
26 {
27     for(int i=0;i<A.len/2;i++)
28     {
29         int tem=A.d[A.len-i-1];
30         A.d[A.len-i-1]=A.d[i];
31         A.d[i]=tem;
32     } 
33     return A;
34 }
35 Bignum Add(Bignum A,Bignum B)
36 {
37     Bignum C;
38     int carry=0;
39     for(int i=0;i<A.len||i<B.len;i++)
40     {
41         C.d[C.len++]=A.d[i]+B.d[i]+carry;
42         carry=C.d[C.len-1]/10;
43         C.d[C.len-1]%=10;
44     }
45     while(carry!=0)
46     {
47         C.d[C.len++]=carry%10;
48         carry/=10;
49     }
50     return C;
51 }
52 int main(int argc, char *argv[])
53 {
54     char ori[20];
55     int K;
56     Bignum A,B;
57     scanf("%s",ori);
58     scanf("%d",&K);
59     int step=0;
60     int len=strlen(ori);
61     for(int i=len-1;i>=0;i--)
62     {
63         A.d[A.len++]=ori[i]-0;
64     }
65     while(!Judge(A)&&step<K)
66     {
67         step++;
68         //进行反转
69         B=Reverse(A);
70         A=Add(A,B);
71     }
72     for(int i=A.len-1;i>=0;i--)
73     {
74         printf("%d",A.d[i]);
75     }
76     putchar(\n);     
77     printf("%d\n",step);
78     return 0;
79 }
View Code

 

PAT1024. Palindromic Number

标签:

原文地址:http://www.cnblogs.com/GoFly/p/4297099.html

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