Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct Elmnt
- {
- double dbl;
- int d;
- int ind;
- } Elmnt;
- int
- cmp(const void *first_val, const void *second_val)
- {
- Elmnt *a = (Elmnt *) first_val;
- Elmnt *b = (Elmnt *) second_val;
- int res = (a->dbl < b->dbl) ? -1 : 1;
- return a->dbl != b->dbl ? res : a->ind - b->ind;
- }
- int
- main(void)
- {
- int sz_current = 1;
- Elmnt *arr = malloc(sz_current * sizeof(Elmnt));
- if (arr == NULL) {
- exit(EXIT_FAILURE);
- }
- int pos = 0;
- while (scanf("%lf%d", &arr[pos].dbl, &arr[pos].d) == 2) {
- arr[pos].ind = pos;
- if (++pos == sz_current) {
- arr = realloc(arr, 2 * sz_current * sizeof(Elmnt));
- sz_current *= 2;
- }
- }
- qsort(arr, pos, sizeof(arr[0]), cmp);
- for (int it = 0; it < pos; ++it) {
- printf("%.10g %d\n", arr[it].dbl, arr[it].d);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment