ProgMe

Untitled

Apr 6th, 2020
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define int int64_t
  3.  
  4. using namespace std;
  5.  
  6. int32_t main()
  7. {
  8. int t;
  9. cin >> t;
  10. while (t--)
  11. {
  12. int n;
  13. vector <int> ans;
  14. cin >> n;
  15. ans.push_back(n);
  16. while (n > 9)
  17. {
  18. string s;
  19. s = to_string(n);
  20. if (s.size() == 2 && s [0] == s [1])
  21. break;
  22. int mid = s.size() / 2 + 1;
  23. if (!(s.size() & 1))
  24. {
  25. string s1 = s.substr(0, mid - 1);
  26. int i = stol(s1);
  27. s1 = s.substr(mid - 1, (int)s.size());
  28. int j = stol(s1);
  29. if (abs(i - j) == 0)
  30. {
  31. s1 = s.substr(0, mid - 2);
  32. i = stol(s1);
  33. s1 = s.substr(mid - 2, (int)s.size());
  34. j = stol(s1);
  35. int ans1 = abs(i - j), ans2;
  36. s1 = s.substr(0, mid);
  37. i = stol(s1);
  38. s1 = s.substr(mid, (int)s.size());
  39. j = stol(s1);
  40. ans2 = abs(i - j);
  41. n = min(ans1, ans2);
  42. }
  43. else
  44. n = abs(i - j);
  45. }
  46. else
  47. {
  48. string s1 = s.substr(0, mid - 1);
  49. int i = stol(s1);
  50. s1 = s.substr(mid - 1, s.size());
  51. int j = stol(s1);
  52. int ans1 = abs(i - j), ans2;
  53. if (ans1 == 0)
  54. ans1 = LLONG_MAX;
  55. s1 = s.substr(0, mid);
  56. i = stol(s1);
  57. s1 = s.substr(mid, s.size());
  58. j = stol(s1);
  59. ans2 = abs(i - j);
  60. if (ans2 == 0)
  61. ans2 = LLONG_MAX;
  62. n = min(ans1, ans2);
  63. }
  64. ans.push_back(n);
  65. }
  66. cout << ans.size() << " ";
  67. for(auto i: ans)
  68. cout << i << " ";
  69. cout << "\n";
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment