fork download
  1. #include <list>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. class Num
  7. {
  8. public:
  9. Num(int n = 0):n(n)
  10. {
  11. d = 0;
  12. // метод ужасный, но вопрос не в нем...
  13. for (int i = 1; i < n; i++)
  14. if (n % i == 0) ++d;
  15. }
  16. int value() const { return n; }
  17. int dividers() const { return d; }
  18. private:
  19. int n, d;
  20. };
  21.  
  22. bool lessD(const Num& a, const Num& b)
  23. {
  24. return a.dividers() < b.dividers();
  25. }
  26.  
  27.  
  28. int main(int argc, const char * argv[])
  29. {
  30. list<Num> l;
  31. for(int i = 0; i < 20; ++i)
  32. l.push_back(20+rand()%200);
  33.  
  34. for(auto x: l) cout << x.value() << " ";
  35. cout << endl;
  36.  
  37. l.sort(lessD);
  38.  
  39. for(auto x: l) cout << x.value() << "(" << x.dividers() <<") ";
  40. cout << endl;
  41.  
  42. }
  43.  
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
203 106 197 135 213 155 206 112 69 41 182 47 110 79 183 146 160 46 192 156 
197(1) 41(1) 47(1) 79(1) 203(3) 106(3) 213(3) 155(3) 206(3) 69(3) 183(3) 146(3) 46(3) 135(7) 182(7) 110(7) 112(9) 160(11) 156(11) 192(13)