fork(1) download
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <vector>
  4. #include <tuple>
  5.  
  6. int cntDiv(int n) //get number of divisors
  7. {
  8. int lim = n;
  9. int c = 0;
  10. if(n == 1)
  11. return 1;
  12. for(int i = 1; i < lim; i++)
  13. {
  14. if(n % i == 0)
  15. {
  16. lim = n / i;
  17. if(lim != i)
  18. c++;
  19. c++;
  20. }
  21. }
  22. return c;
  23. }
  24.  
  25. std::vector<std::pair<int, int>> customSort(const std::vector<int>& v)
  26. {
  27. std::vector<std::pair<int, int>> ps;
  28. ps.reserve(v.size());
  29.  
  30. // We don't have zip sort :/
  31. // So building the pair
  32. for (auto e : v)
  33. {
  34. ps.emplace_back(e, cntDiv(e));
  35. }
  36. std::sort(ps.begin(), ps.end(), [](const auto&lhs, const auto& rhs) {
  37. // descending number of divisors, increasing value
  38. return std::make_tuple(-lhs.second, lhs.first)
  39. < std::make_tuple(-rhs.second, rhs.first);
  40. });
  41. return ps;
  42. }
  43.  
  44. int main()
  45. {
  46. const std::vector<int> v = {12, 20, 4, 100, 13};
  47. const auto res = customSort(v);
  48.  
  49. for(const auto& p : res)
  50. {
  51. std::cout << p.first << " ";
  52. }
  53. std::cout << std::endl;
  54. }
Success #stdin #stdout 0s 3416KB
stdin
Standard input is empty
stdout
100 12 20 4 13