Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <cstdlib>
- #include <cstring>
- #include <cassert>
- #define _CRT_SECURE_NO_WARNINGS
- struct stud
- {
- int id;
- char name[33];
- int mark;
- };
- void bubble_sort(stud *a, int n)
- {
- stud t;
- for (int i = 0; i < n; ++i)
- {
- for (int j = 0; j < n - i - 1; ++j)
- {
- if (strcmp(a[j].name, a[j + 1].name) > 0)
- {
- t = a[j];
- a[j] = a[j + 1];
- a[j + 1] = t;
- }
- }
- }
- }
- int binsearch(char* a, stud* arr, int n)
- {
- int l = 0, r = n, m = 0;
- while (r > l)
- {
- m = (l + r) / 2;
- if (strcmp(a, arr[m].name) <= 0)
- r = m;
- else
- l = m + 1;
- }
- return arr[r].mark;
- }
- int main()
- {
- int n = 0, m = 0;
- char a[33] = { 0 }, q[5];
- scanf("%d", &n);
- stud* arr = (stud*)malloc(n*sizeof(stud));
- if (!arr)
- return 1;
- int from = 0, to = 0;
- for (int i = 0; i < n; ++i)
- {
- scanf("%i%n%s%n", &arr[i].id, &from, arr[i].name, &to);
- scanf("%s%i", arr[i].name + to - from, &arr[i].mark);
- arr[i].name[to - from - 1] = ' ';
- }
- bubble_sort(arr, n);
- scanf("%d", &m);
- for (int i = 0; i < m; ++i)
- {
- scanf("%s%n", a, &to);
- scanf("%s", a + to);
- a[to - 1] = ' ';
- printf("%d\n", binsearch(a, arr, n));
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment