The_Law

Untitled

Oct 2nd, 2018
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.93 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct Elmnt
  5. {
  6.     double dbl;
  7.     int d;
  8.     int ind;
  9. } Elmnt;
  10.  
  11. int
  12. cmp(const void *first_val, const void *second_val)
  13. {
  14.     Elmnt *a = (Elmnt *) first_val;
  15.     Elmnt *b = (Elmnt *) second_val;
  16.     int res = (a->dbl < b->dbl) ? -1 : 1;
  17.     return a->dbl != b->dbl ? res : a->ind - b->ind;
  18. }
  19.  
  20. int
  21. main(void)
  22. {
  23.     int sz_current = 1;
  24.     Elmnt *arr = malloc(sz_current * sizeof(Elmnt));
  25.     if (arr == NULL) {
  26.         exit(EXIT_FAILURE);
  27.     }
  28.  
  29.  
  30.     int pos = 0;
  31.  
  32.     while (scanf("%lf%d", &arr[pos].dbl, &arr[pos].d) == 2) {
  33.         arr[pos].ind = pos;
  34.  
  35.         if (++pos == sz_current) {
  36.             arr = realloc(arr, 2 * sz_current * sizeof(Elmnt));
  37.             sz_current *= 2;
  38.         }
  39.     }
  40.  
  41.     qsort(arr, pos, sizeof(arr[0]), cmp);
  42.  
  43.     for (int it = 0; it < pos; ++it) {
  44.         printf("%.10g %d\n", arr[it].dbl, arr[it].d);
  45.     }
  46.     return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment