fork download
  1. #include <algorithm>
  2. #include <vector>
  3. #include <iostream>
  4. using namespace std;
  5. class BigInt;
  6. vector<BigInt*> World;
  7. void WorldEnd()
  8. {
  9. int Z=World.size();
  10. for (int i=0; i<Z; i++)
  11. {
  12. delete World[i];
  13. }
  14. World.clear();
  15. }
  16. class BigInt
  17. {
  18. public:
  19. vector<int> Me;
  20. BigInt();
  21. BigInt(int inn);
  22. BigInt(vector<int> inn);
  23. BigInt reverse();
  24. BigInt* add(BigInt* that);
  25. void print();
  26. bool isLoop();
  27. //~BigInt();
  28. BigInt& operator=(const BigInt&);
  29. };
  30.  
  31. ostream& operator<<(ostream& os, const BigInt& object)
  32. {
  33. for(int i=object.Me.size()-1; i>=0 ; i--)
  34. {
  35. os<<object.Me[i];
  36. }
  37. return os;
  38. }
  39. BigInt& BigInt::operator =(const BigInt& rhs)
  40. {
  41. Me.clear();
  42. for (int i=0; i<rhs.Me.size(); i++)
  43. {
  44. Me.push_back(rhs.Me[i]);
  45. }
  46. return *this;
  47. }
  48. bool BigInt::isLoop()
  49. {
  50. if (Me.size()==1)
  51. return true;
  52. if (Me.size()>=2)
  53. {
  54. if (Me.size()%2==0)
  55. {
  56. for (int i=0; i<= Me.size()/2 ; i++)
  57. {
  58. if (Me[i]!= Me[ Me.size()-1-i])
  59. return false;
  60. }
  61. return true;
  62. }else
  63. {
  64. for (int i=0; i<=(Me.size()-1)/2; i++)
  65. {
  66. if (Me[i]!= Me[ Me.size()-1-i])
  67. return false;
  68. }
  69. return true;
  70. }
  71. }
  72. return false;
  73. }
  74. BigInt* BigInt::add(BigInt* that)
  75. {
  76. vector<int> dest;
  77. int carry=0;
  78. for(int i=0; i<that->Me.size(); i++)
  79. {
  80. int sum= Me[i] + that->Me[i]+carry;
  81. if (sum>=10)
  82. {
  83. carry=1; dest.push_back( sum-10);
  84. }else {
  85. carry=0; dest.push_back( sum);
  86. }
  87. }
  88. if (carry==1)
  89. dest.push_back(1);
  90. BigInt* ret=new BigInt(dest);
  91. World.push_back(ret);
  92. return ret;
  93. }
  94. BigInt operator+(const BigInt& lhs,const BigInt& rhs)
  95. {
  96. // BigInt* ret= (&lhs)->add(&rhs);
  97. // return (*ret);
  98. //////////////////////////////////////
  99. vector<int> dest;
  100. int carry=0;
  101. for(int i=0; i<lhs.Me.size(); i++)
  102. {
  103. int sum= lhs.Me[i] + rhs.Me[i]+carry;
  104. if (sum>=10)
  105. {
  106. carry=1; dest.push_back( sum-10);
  107. }else {
  108. carry=0; dest.push_back( sum);
  109. }
  110. }
  111. if (carry==1)
  112. dest.push_back(1);
  113. BigInt ret(dest);
  114. return (ret);
  115. }
  116.  
  117. BigInt BigInt::reverse()
  118. {
  119. vector<int> hand;
  120. for (int i=Me.size()-1; i>=0; i--)
  121. hand.push_back(Me[i]);
  122. BigInt ret(hand);
  123. return ret;
  124. }
  125. BigInt::BigInt(int inn)
  126. {
  127. while(inn>=10)
  128. {
  129. Me.push_back(inn%10);
  130. inn /=10;
  131. }
  132. Me.push_back(inn);
  133. }
  134. BigInt::BigInt(vector<int> inn)
  135. {
  136. for (int i=0; i<inn.size(); i++)
  137. {
  138. Me.push_back(inn[i]);
  139. }
  140.  
  141. }
  142. BigInt::BigInt(){}
  143.  
  144. void BigInt::print()
  145. {
  146. for (int i=Me.size()-1; i>=0; i--)
  147. {
  148. cout<< Me[i];
  149. }
  150. }
  151.  
  152. void ACM10018(int P)
  153. {
  154.  
  155. BigInt P1(P);
  156. BigInt P2;
  157. BigInt P3;
  158.  
  159. int Step=0;
  160. do
  161. { P2=P1.reverse();
  162. P3=P1+P2;
  163.  
  164. Step+=1;
  165. if (!P3.isLoop())
  166. P1=P3;
  167. else
  168. break;
  169. }while(true);
  170. cout<< Step<<" "<<(P3)<<endl;
  171.  
  172. }
  173. int main()
  174. {
  175. int P,N;
  176. cin>>N;
  177. if (N>0 && N<=100)
  178. {
  179. for (int i=0; i<N; i++)
  180. {
  181. cin>>P;
  182. ACM10018(P);
  183. WorldEnd();
  184. }
  185. }
  186.  
  187. return 0;
  188. }
  189.  
Success #stdin #stdout 0.01s 2688KB
stdin
Standard input is empty
stdout
Standard output is empty