Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. int cif(long a, int b);
  7. int PalMinim(long n);
  8. int Nrcifre(long n);
  9.  
  10. int main()
  11. {
  12. int n;
  13. cin >> n;
  14. cout << PalMinim(n);
  15. }
  16.  
  17. int cif(long a, int b)
  18. {
  19. int cnt = 0;
  20. if(!a && !b) cnt = 1;
  21. while(a)
  22. {
  23. if(a % 10 == b) cnt++;
  24. a/=10;
  25. }
  26. return cnt;
  27. }
  28.  
  29. int PalMinim(long n)
  30. {
  31.  
  32. int m = 0, nrap, i, c;
  33. if(Nrcifre(n) % 2 == 0)
  34. {
  35. for(c = 9; c >= 0; c--)
  36. {
  37. nrap = cif(n,c);
  38. if(nrap % 2 != 0)
  39. return 0;
  40. else
  41. for(i = 1; i<=nrap/2; i++)
  42. m = m * 10 + c;
  43. }
  44. }
  45. else
  46. {
  47. int ok = - 1;
  48. for(c = 9; c >= 0, ok <= 0; c--)
  49. {
  50. nrap = cif(n,c);
  51. if(nrap % 2 == 1)
  52. {ok++;
  53. if(nrap == 1) m = m * 10 + c;
  54. else
  55. {
  56. for(i = 1; i<=nrap/2; i++)
  57. m = m * 10 + c;
  58. }
  59. }
  60. else
  61. {
  62. for(i = 1; i<=nrap/2; i++)
  63. m = m * 10 + c;
  64. }
  65. return 0;
  66. }
  67. }
  68. n = m;
  69. while(m)
  70. {
  71. n = n * 10 + m % 10;
  72. m /= 10;
  73. }
  74. return n;
  75. }
  76.  
  77. int Nrcifre(long n)
  78. {
  79. int nr = 0;
  80. if(n == 0) nr = 1;
  81. while(n)
  82. {
  83. n++;
  84. n/=10;
  85. }
  86. return nr;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement