70 lines
1.9 KiB
C
70 lines
1.9 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#define MAX_NAME_LENGTH 100
|
|
|
|
typedef struct {
|
|
char name[MAX_NAME_LENGTH];
|
|
int votes;
|
|
} Student;
|
|
|
|
int compare_students(const void *a, const void *b) {
|
|
const Student *studentA = (const Student *)a;
|
|
const Student *studentB = (const Student *)b;
|
|
|
|
if (studentA->votes != studentB->votes) {
|
|
return studentB->votes - studentA->votes; // Сортировка по убыванию голосов
|
|
} else {
|
|
return strcmp(studentA->name, studentB->name); // Лексикографическая сортировка по имени
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
Student students[100];
|
|
int student_count = 0;
|
|
char line[256];
|
|
|
|
while (fgets(line, sizeof(line), stdin)) {
|
|
int votes;
|
|
char name[MAX_NAME_LENGTH];
|
|
|
|
if (sscanf(line, "%d %[^\n]", &votes, name) != 2) {
|
|
break; // Прерываем чтение при неверном формате
|
|
}
|
|
|
|
// Поиск студента в массиве
|
|
int found = 0;
|
|
for (int i = 0; i < student_count; i++) {
|
|
if (strcmp(students[i].name, name) == 0) {
|
|
students[i].votes += votes;
|
|
found = 1;
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Если студент не найден, добавляем нового
|
|
if (!found) {
|
|
strcpy(students[student_count].name, name);
|
|
students[student_count].votes = votes;
|
|
student_count++;
|
|
}
|
|
}
|
|
|
|
if (student_count == 0) {
|
|
printf("Chyba: nepodarilo sa nacitat ziadny zaznam.\n");
|
|
return 1;
|
|
}
|
|
|
|
// Сортировка студентов
|
|
qsort(students, student_count, sizeof(Student), compare_students);
|
|
|
|
// Вывод результатов
|
|
printf("Vysledky:\n");
|
|
for (int i = 0; i < student_count; i++) {
|
|
printf("%d %s\n", students[i].votes, students[i].name);
|
|
}
|
|
|
|
return 0;
|
|
}
|