fork download
  1. #include <iostream>
  2. #include <set>
  3. #include <vector>
  4. #include <map>
  5. #include <climits>
  6. #include <algorithm>
  7. using namespace std;
  8.  
  9. int countEven(long long n) {
  10. int c = 0;
  11. while (n != 0) {
  12. int digit = n % 10;
  13. if (digit % 2 == 0) {
  14. c++;
  15. }
  16. n = n / 10;
  17. }
  18. return c;
  19. }
  20.  
  21. int countOdd(long long n) {
  22. int c = 0;
  23. while (n != 0) {
  24. int digit = n % 10;
  25. if (digit % 2 != 0) {
  26. c++;
  27. }
  28. n = n / 10;
  29. }
  30. return c;
  31. }
  32.  
  33.  
  34.  
  35. int main() {
  36.  
  37. int n;cin >> n;
  38. vector<int> v1, v2;
  39.  
  40. for (int i = 0;i < n;i++) {
  41. int value;cin >> value;
  42. v1.push_back(value);
  43. v2.push_back(value);
  44. }
  45.  
  46. sort(v1.begin(), v1.end(), [](int a, int b) {
  47. int ca = countEven(a);
  48. int cb = countEven(b);
  49.  
  50. if (ca != cb) {
  51. return ca < cb;
  52. }
  53. else {
  54. return a < b;
  55. }
  56. });
  57.  
  58. for (auto value : v1) {
  59. cout << value << " ";
  60. }
  61.  
  62. cout << endl;
  63. stable_sort(v2.begin(), v2.end(), [](int a, int b) {
  64. int ca = countOdd(a);
  65. int cb = countOdd(b);
  66.  
  67. return ca < cb;
  68.  
  69. });
  70.  
  71. for (auto value : v2) {
  72. cout << value << " ";
  73. }
  74. return 0;
  75. }
  76.  
  77.  
Success #stdin #stdout 0s 544KB
stdin
Standard input is empty
stdout