loulett

HW1_stud

Nov 11th, 2014
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.20 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <cassert>
  5. #define _CRT_SECURE_NO_WARNINGS
  6. struct stud
  7. {
  8.     int id;
  9.     char name[33];
  10.     int mark;
  11. };
  12.  
  13. void bubble_sort(stud *a, int n)
  14. {
  15.     stud t;
  16.     for (int i = 0; i < n; ++i)
  17.     {
  18.         for (int j = 0; j < n - i - 1; ++j)
  19.         {
  20.             if (strcmp(a[j].name, a[j + 1].name) > 0)
  21.             {
  22.                 t = a[j];
  23.                 a[j] = a[j + 1];
  24.                 a[j + 1] = t;
  25.             }
  26.         }
  27.     }
  28. }
  29.  
  30. int binsearch(char* a, stud* arr, int n)
  31. {
  32.     int l = 0, r = n, m = 0;
  33.     while (r > l)
  34.     {
  35.         m = (l + r) / 2;
  36.         if (strcmp(a, arr[m].name) <= 0)
  37.             r = m;
  38.         else
  39.             l = m + 1;
  40.     }
  41.     return arr[r].mark;
  42. }
  43.  
  44. int main()
  45. {
  46.     int n = 0, m = 0;
  47.     char a[33] = { 0 }, q[5];
  48.     scanf("%d", &n);
  49.     stud* arr = (stud*)malloc(n*sizeof(stud));
  50.     if (!arr)
  51.         return 1;
  52.  
  53.     int from = 0, to = 0;
  54.     for (int i = 0; i < n; ++i)
  55.     {
  56.         scanf("%i%n%s%n", &arr[i].id, &from, arr[i].name, &to);
  57.         scanf("%s%i", arr[i].name + to - from, &arr[i].mark);
  58.         arr[i].name[to - from - 1] = ' ';
  59.     }
  60.  
  61.  
  62.     bubble_sort(arr, n);
  63.  
  64.     scanf("%d", &m);
  65.  
  66.     for (int i = 0; i < m; ++i)
  67.     {
  68.         scanf("%s%n", a, &to);
  69.         scanf("%s", a + to);
  70.         a[to - 1] = ' ';
  71.  
  72.         printf("%d\n", binsearch(a, arr, n));
  73.     }
  74.  
  75.  
  76.  
  77.     return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment