Advertisement
cd62131

max of person's bids

Apr 5th, 2019
551
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.84 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define N 10000
  5. int main(void) {
  6.   FILE *in = fopen("bids.txt", "r");
  7.   if (!in) {
  8.     exit(1);
  9.   }
  10.   char *names[N];
  11.   int bids[N] = {0};
  12.   int used = 0;
  13.   for (char buf[BUFSIZ]; fgets(buf, BUFSIZ, in);) {
  14.     char name[BUFSIZ];
  15.     int bid;
  16.     if (sscanf(buf, "%s%d", name, &bid) == 2) {
  17.       int i = 0;
  18.       for (; i < used; ++i) {
  19.         if (!strcmp(names[i], name)) {
  20.           break;
  21.         }
  22.       }
  23.       if (i == used) {
  24.         names[used++] = strdup(name);
  25.       }
  26.       if (bids[i] < bid) {
  27.         bids[i] = bid;
  28.       }
  29.     }
  30.   }
  31.   fclose(in);
  32.   FILE *out = fopen("output.txt", "w");
  33.   if (!out) {
  34.     exit(1);
  35.   }
  36.   for (int i = 0; i < used; ++i) {
  37.     fprintf(out, "%s %d\n", names[i], bids[i]);
  38.     free(names[i]);
  39.   }
  40.   fclose(out);
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement