Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.03 KB | None | 0 0
  1. #include<iostream>
  2.  
  3.  
  4. struct Date {
  5.     int day;
  6.     int month;
  7.     int year;
  8. };
  9.  
  10. int compare (Date date1, Date date2) {
  11.     if (date1.year != date2.year) {
  12.         if (date1.year > date2.year) {
  13.             return 1;
  14.         }
  15.         if (date1.year < date2.year) {
  16.             return -1;
  17.         }
  18.     }
  19.     else if (date1.month != date2.month) {
  20.         if (date1.month > date2.month) {
  21.             return 1;
  22.         }
  23.         if (date1.month < date2.month) {
  24.             return -1;
  25.         }
  26.     }
  27.     else if (date1.day != date2.day) {
  28.             if (date1.day > date2.day) {
  29.                 return 1;
  30.             }
  31.             if (date1.day < date2.day) {
  32.                 return -1;
  33.             }
  34.     }
  35.     else {
  36.         return 0;
  37.     }
  38. }
  39.  
  40. void mySwap (Date& d1, Date& d2) {
  41.     Date buf;
  42.     buf.day = d2.day;
  43.     buf.month = d2.month;
  44.     buf.year = d2.year;
  45.  
  46.     d2.day = d1.day;
  47.     d2.month = d1.month;
  48.     d2.year = d1.year;
  49.  
  50.     d1.day = buf.day;
  51.     d1.month = buf.month;
  52.     d1.year = buf.year;
  53. }
  54.  
  55. void heapify(Date* a, Date* b, int i, int n) {
  56.     int largest = i;
  57.     int left = 2 * i + 1;
  58.     int right = 2 * i + 2;
  59.     if((left < n) && (compare(a[left], a[largest]) == 1)) {
  60.         largest = left;
  61.     }
  62.     if((right < n) && (compare(a[right], a[largest]) == 1)) {
  63.         largest = right;
  64.     }
  65.     if(largest != i){
  66.         mySwap(a[i], a[largest]);
  67.         mySwap(b[i], b[largest]);
  68.         heapify(a, b, largest, n);
  69.     }
  70. }
  71.  
  72. void buildHeap(Date* a, Date* b, int n){
  73.     for(int i = n / 2 -1; i >= 0; i--) {
  74.         heapify(a, b, i, n);
  75.     }
  76. }
  77.  
  78. void heapSort(Date* a, Date* b, int n) {
  79.     buildHeap(a, b, n);
  80.     for(int i = n - 1; i >= 1; i--) {
  81.         mySwap(a[0], a[i]);
  82.         mySwap(b[0], b[i]);
  83.         heapify(a, b, 0, i);
  84.     }
  85. }
  86.  
  87. bool isAdult(Date b, Date d) {
  88.     if (d.year - b.year > 18) {
  89.         return true;
  90.     }
  91.     else if (d.year - b.year == 18) {
  92.         if (d.month > b.month) {
  93.             return true;
  94.         }
  95.         else if (d.month == b.month) {
  96.             if (d.day > b.day) {
  97.                 return true;
  98.             }
  99.             else
  100.                 return false;
  101.         }
  102.         else
  103.             return false;
  104.     }
  105.     else
  106.         return false;
  107. }
  108.  
  109. int maxAge(Date b, Date d) {
  110.     int result = d.year - b.year;
  111.     if (d.month < b.month) {
  112.         result--;
  113.     }
  114.     if (d.month == b.month) {
  115.         if (d.day < b.day) {
  116.             result--;
  117.         }
  118.     }
  119.     return result;
  120. }
  121.  
  122. int main() {
  123.     int n;
  124.     int cnt = 0;
  125.     std::cin >> n;
  126.     Date* left = new Date[n];
  127.     Date* right = new Date[n];
  128.     for (int i = 0; i < n; i++) {
  129.         Date birth,death;
  130.         std::cin >> birth.day >> birth.month >> birth.year;
  131.         std::cin >> death.day >> death.month >> death.year;
  132.         if (isAdult(birth, death)) {
  133.             Date buf;
  134.             buf = birth;
  135.             buf.year += 18;
  136.             left[cnt] = buf;
  137.             if (maxAge(birth, death) < 80) {
  138.                 right[cnt] = death;
  139.             }
  140.             else {
  141.                 buf = birth;
  142.                 buf.year += 80;
  143.                 buf.day--;
  144.                 right[cnt] = buf;
  145.             }
  146.             cnt++;
  147.         }
  148.     }
  149.  
  150.     heapSort(left, right, cnt);
  151.    
  152.  
  153.     for (int i = 0; i < cnt; i++) {
  154.         std::cout << left[i].day <<' '<<left[i].month<<' '<< left[i].year<<' '
  155.         <<right[i].day<<' '<< right[i].month <<' '<< right[i].year<<std::endl;
  156.     }
  157.     delete[] left;
  158.     delete[] right;
  159.     return 0;
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement