vakho

ალგ. – ლაბ. N7 – 3;

Jun 10th, 2012
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <ctime>
  5.  
  6. using namespace std;
  7.  
  8. class Human
  9. {
  10. public:
  11.     string Name, Surname;
  12.     int Age;
  13.     Human(){};
  14.     Human(string N, string S, int A):Name(N), Surname(S), Age(A){};
  15.     ~Human(){};
  16. };
  17.  
  18. int MaxAge(const vector<Human> vec)
  19. {
  20.     Human Max = vec[0];
  21.     for (int i = 1; i < vec.size(); i++)
  22.         if (vec[i].Age > Max.Age)
  23.             Max = vec[i];
  24.     return Max.Age;
  25. };
  26.  
  27. void countingSort(const vector<Human> &a, vector<Human> &b)
  28. {
  29.     int k = MaxAge(a);
  30.     k++;
  31.  
  32.     vector<int> c(k);
  33.  
  34.     for (int i = 0; i < a.size(); i++)
  35.         c[ a[i].Age ]++;
  36.     for (int i = 1; i < k; i++)
  37.         c[i] += c[i - 1];
  38.  
  39.     for (int i = a.size() - 1; i >= 0; i--)
  40.     {
  41.         b[ c[ a[i].Age ] - 1 ] = a[i];
  42.         c[ a[i].Age ]--;
  43.     };
  44. };
  45.  
  46. int main()
  47. {
  48.     system("COLOR F0");
  49.     srand( time(NULL) );
  50.  
  51.     vector<Human> a;
  52.  
  53.     /*
  54.     a ვექტორის შევსება ფაილიდან ან ხელით
  55.     ...
  56.     */
  57.  
  58.     vector<Human> b(a.size());
  59.  
  60.     for (int i = 0; i < a.size(); i++)
  61.         cout << " a [ " << i << " ]\t= " << a[i].Age << endl;
  62.     cout << endl;
  63.  
  64.     countingSort(a, b);
  65.  
  66.     for (int i = 0; i < b.size(); i++)
  67.         cout << " b [ " << i << " ]\t= " << b[i].Age << endl;
  68.     cout << endl;
  69.  
  70.     system("PAUSE");
  71.     return (0);
  72. };
Advertisement
Add Comment
Please, Sign In to add comment