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

PAT 1024. Palindromic Number (25)

时间:2015-08-07 12:51:42      阅读:306      评论:0      收藏:0      [点我收藏+]

标签:

1024. Palindromic Number (25)

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

以下的代码由于采用了一个自己编写的High-Precision的类,并且在类中重载了各种运算符,因而显得代码比较冗长

 

  1 #include <string>
  2 #include <iostream>
  3 #include <sstream>
  4 
  5 using namespace std;
  6 
  7 class HPrecision
  8 {
  9 private:
 10     std::string num;
 11 public:
 12     HPrecision(){}
 13     ~HPrecision(){}
 14     HPrecision(std::string num){ this->num = num; }
 15     HPrecision(long long integer);
 16     HPrecision& operator=(const HPrecision &hp){ this->num = hp.num; return *this; }
 17     friend std::istream& operator>>(std::istream &is, HPrecision &in);
 18     friend std::ostream& operator<<(std::ostream &os, const HPrecision &out);
 19     friend HPrecision operator+(const HPrecision &lhs, const HPrecision &rhs);
 20     friend HPrecision operator-(const HPrecision &lhs, const HPrecision &rhs);
 21     friend HPrecision operator*(const HPrecision &lhs, const HPrecision &rhs);
 22     friend HPrecision operator/(const HPrecision &lhs, const HPrecision &rhs);
 23     HPrecision& operator+=(const HPrecision &hp){ *this = *this + hp; return *this; }
 24     HPrecision& operator-=(const HPrecision &hp){ *this = *this - hp; return *this; }
 25     friend bool operator<(const HPrecision &lhs, const HPrecision &rhs);
 26     friend bool operator<=(const HPrecision &lhs, const HPrecision &rhs);
 27     friend bool operator>(const HPrecision &lhs, const HPrecision &rhs);
 28     friend bool operator>=(const HPrecision &lhs, const HPrecision &rhs);
 29     friend bool operator==(const HPrecision &lhs, const HPrecision &rhs);
 30     friend bool operator!=(const HPrecision &lhs, const HPrecision &rhs);
 31     HPrecision reverse();
 32 };
 33 
 34 HPrecision::HPrecision(long long integer)
 35 {
 36     std::stringstream ss;
 37     ss << integer;
 38     this->num = ss.str();
 39 }
 40 
 41 std::istream& operator>>(std::istream &is, HPrecision &in)
 42 {
 43     is >> in.num;
 44     return is;
 45 }
 46 
 47 std::ostream& operator<<(std::ostream &os, const HPrecision &out)
 48 {
 49     os << out.num;
 50     return os;
 51 }
 52 
 53 HPrecision operator+(const HPrecision &lhs, const HPrecision &rhs)
 54 {
 55     HPrecision sum;
 56     HPrecision op1, op2;
 57     op1 = lhs;
 58     op2 = rhs;
 59 
 60     if (op1.num[0] == -&&op2.num[0] == -)    //all negative
 61     {
 62         op1.num.erase(op1.num.begin());
 63         op2.num.erase(op2.num.begin());
 64         sum = op1 + op2;
 65         sum.num = std::string("-") + sum.num;
 66         return sum;
 67     }
 68     else if (op1.num[0] == -)    //left operand is negative
 69     {
 70         op1.num.erase(op1.num.begin());
 71         return op2 - op1;
 72     }
 73     else if (op2.num[0] == -)    //right operand is negative
 74     {
 75         op2.num.erase(op2.num.begin());
 76         return op1 - op2;
 77     }
 78 
 79     int i = op1.num.size() - 1;
 80     int j = op2.num.size() - 1;
 81     int carry = 0;
 82     while (i >= 0 || j >= 0)
 83     {
 84         if (i >= 0 && j >= 0)
 85         {
 86             int sumdigit;
 87             sumdigit = (op1.num[i] - 0) + (op2.num[j] - 0) + carry;
 88             sum.num = std::string(1, sumdigit % 10 + 0) + sum.num;
 89             carry = sumdigit / 10;
 90             i--;
 91             j--;
 92         }
 93         else if (i >= 0)
 94         {
 95             int sumdigit;
 96             sumdigit = (op1.num[i] - 0) + carry;
 97             sum.num = std::string(1, sumdigit % 10 + 0) + sum.num;
 98             carry = sumdigit / 10;
 99             i--;
100         }
101         else
102         {
103             int sumdigit;
104             sumdigit = (op2.num[j] - 0) + carry;
105             sum.num = std::string(1, sumdigit % 10 + 0) + sum.num;
106             carry = sumdigit / 10;
107             j--;
108         }
109     }
110     if (carry != 0)
111         sum.num = std::string("1") + sum.num;
112 
113     return sum;
114 }
115 
116 HPrecision operator-(const HPrecision &lhs, const HPrecision &rhs)
117 {
118     HPrecision diff;
119     HPrecision op1, op2;
120     op1 = lhs;
121     op2 = rhs;
122 
123     if (op1.num[0] == -&&op2.num[0] == -)    //all negative
124     {
125         op1.num.erase(op1.num.begin());
126         op2.num.erase(op2.num.begin());
127         return op2 - op1;
128     }
129     else if (op1.num[0] == -)    //left operand is negative
130     {
131         op1.num.erase(op1.num.begin());
132         diff = op2 + op1;
133         diff.num = std::string("-") + diff.num;
134         return diff;
135     }
136     else if (op2.num[0] == -)    //right operand is negative
137     {
138         op2.num.erase(op2.num.begin());
139         return op1 + op2;
140     }
141     if (op1.num.size() < op2.num.size() || (op1.num.size() == op2.num.size() && op1.num < op2.num))    //small - big
142     {
143         diff = op2 - op1;
144         diff.num = std::string("-") + diff.num;
145         return diff;
146     }
147     if (op1.num == op2.num)
148         return HPrecision(std::string("0"));
149 
150     while (op2.num.size() < op1.num.size())
151         op2.num = std::string("0") + op2.num;
152     int borrow = 0;
153     int i = op1.num.size() - 1;
154     while (i >= 0)
155     {
156         int diffdigit;
157         diffdigit = op1.num[i] - op2.num[i] - borrow;
158         if (diffdigit < 0)
159         {
160             diffdigit += 10;
161             borrow = 1;
162         }
163         else
164             borrow = 0;
165         diff.num = std::string(1, diffdigit + 0) + diff.num;
166         i--;
167     }
168     std::string::iterator it = diff.num.begin();
169     while (*it == 0)
170         diff.num.erase(it);
171 
172     return diff;
173 }
174 
175 HPrecision operator*(const HPrecision &lhs, const HPrecision &rhs)
176 {
177     HPrecision mul = 0;
178     HPrecision op1, op2;
179     op1 = lhs; op2 = rhs;
180     if (op1.num[0] == -&&op2.num[0] == -)    //all negative
181     {
182         op1.num.erase(op1.num.begin());
183         op2.num.erase(op2.num.begin());
184         return op1 * op2;
185     }
186     else if (op1.num[0] == -)    //left operand is negative
187     {
188         op1.num.erase(op1.num.begin());
189         mul = op1 * op2;
190         mul.num = std::string("-") + mul.num;
191         return mul;
192     }
193     else if (op2.num[0] == -)    //right operand is negative
194     {
195         op2.num.erase(op2.num.begin());
196         mul = op1 * op2;
197         mul.num = std::string("-") + mul.num;
198         return mul;
199     }
200     int i = 0;
201     while (i < op2.num.size())
202     {
203 
204         HPrecision tmp = 0;
205         for (int j = 0; j < op2.num[i]; j++)
206             tmp += op1;
207 
208     }
209 }
210 
211 bool operator<(const HPrecision &lhs, const HPrecision &rhs)
212 {
213     return lhs.num < rhs.num;
214 }
215 bool operator<=(const HPrecision &lhs, const HPrecision &rhs)
216 {
217     return lhs.num <= rhs.num;
218 }
219 bool operator>(const HPrecision &lhs, const HPrecision &rhs)
220 {
221     return lhs.num > rhs.num;
222 }
223 bool operator>=(const HPrecision &lhs, const HPrecision &rhs)
224 {
225     return lhs.num >= rhs.num;
226 }
227 bool operator==(const HPrecision &lhs, const HPrecision &rhs)
228 {
229     return lhs.num == rhs.num;
230 }
231 bool operator!=(const HPrecision &lhs, const HPrecision &rhs)
232 {
233     return lhs.num != rhs.num;
234 }
235 
236 HPrecision HPrecision::reverse()
237 {
238     std::string num = this->num;
239 
240     std::string reversenum;
241     reversenum.assign(num.rbegin(), num.rend());
242 
243     return HPrecision(reversenum);
244 }
245 bool Is_Palindromic(HPrecision num)
246 {
247     HPrecision reversenum = num.reverse();
248     if (reversenum == num)
249         return true;
250     return false;
251 }
252 int main()
253 {
254     HPrecision num;
255     int steps;
256     cin >> num >> steps;
257 
258     for (int i = 0; i < steps; i++)
259     {
260         if (Is_Palindromic(num))
261         {
262             cout << num << "\n" << i;
263             return 0;
264         }
265         num = num.reverse() + num;
266     }
267     cout << num << "\n" << steps;
268 }

 

PAT 1024. Palindromic Number (25)

标签:

原文地址:http://www.cnblogs.com/jackwang822/p/4710237.html

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