2024-03-21 22:47:06 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2024-03-21 23:18:00 +00:00
|
|
|
#define MAX_SIZE 100
|
|
|
|
#define MAX_STUDENTS 100
|
|
|
|
|
|
|
|
// Štruktúra pre reprezentáciu študenta
|
|
|
|
struct Student {
|
|
|
|
char name[MAX_SIZE];
|
|
|
|
int votes;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Porovnávacia funkcia pre triedenie študentov
|
|
|
|
int compare(const void *p1, const void *p2) {
|
|
|
|
const struct Student *s1 = (const struct Student *)p1;
|
|
|
|
const struct Student *s2 = (const struct Student *)p2;
|
|
|
|
// Porovnanie počtu hlasov
|
|
|
|
if (s1->votes != s2->votes) {
|
|
|
|
return s2->votes - s1->votes; // Zoradenie zostupne podľa hlasov
|
2024-03-21 23:09:53 +00:00
|
|
|
}
|
2024-03-21 23:18:00 +00:00
|
|
|
// Ak majú rovnaký počet hlasov, zoradíme lexikograficky podľa mena
|
|
|
|
return strcmp(s1->name, s2->name);
|
2024-03-21 23:09:53 +00:00
|
|
|
}
|
|
|
|
|
2024-03-22 07:27:46 +00:00
|
|
|
// Načíta študentov zo štandardného vstupu do pola
|
2024-03-22 07:19:34 +00:00
|
|
|
int read_students(struct Student students[], int max_students) {
|
2024-03-21 23:04:41 +00:00
|
|
|
int num_students = 0;
|
2024-03-21 23:18:00 +00:00
|
|
|
char line[MAX_SIZE];
|
2024-03-22 07:19:34 +00:00
|
|
|
while (fgets(line, sizeof(line), stdin) != NULL) {
|
2024-03-21 23:18:00 +00:00
|
|
|
char name[MAX_SIZE];
|
2024-03-22 07:35:26 +00:00
|
|
|
int votes;
|
|
|
|
// Načítame počet hlasov
|
|
|
|
votes = (int)strtol(line, &line, 10);
|
|
|
|
// Preskočíme medzeru
|
|
|
|
line++;
|
|
|
|
// Načítame meno študenta
|
|
|
|
memcpy(name, line, strlen(line) - 1); // Odstránime znak nového riadka
|
|
|
|
name[strlen(line) - 1] = '\0'; // Nastavíme koniec reťazca
|
|
|
|
// Prejdeme všetkých študentov a ak nájdeme rovnaké meno, pripočítame hlasy
|
|
|
|
int found = 0;
|
|
|
|
for (int i = 0; i < num_students; i++) {
|
|
|
|
if (strcmp(students[i].name, name) == 0) {
|
|
|
|
students[i].votes += votes;
|
|
|
|
found = 1;
|
|
|
|
break;
|
2024-03-22 07:30:52 +00:00
|
|
|
}
|
2024-03-22 07:35:26 +00:00
|
|
|
}
|
|
|
|
// Ak sme nenašli rovnaké meno, pridáme nového študenta
|
|
|
|
if (!found) {
|
|
|
|
strcpy(students[num_students].name, name);
|
|
|
|
students[num_students].votes = votes;
|
|
|
|
num_students++;
|
|
|
|
if (num_students >= max_students) {
|
|
|
|
break; // Prekročený maximálny počet študentov
|
2024-03-21 23:20:05 +00:00
|
|
|
}
|
2024-03-21 23:04:41 +00:00
|
|
|
}
|
2024-03-21 22:47:06 +00:00
|
|
|
}
|
2024-03-21 23:20:05 +00:00
|
|
|
return num_students;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
struct Student students[MAX_STUDENTS];
|
2024-03-22 07:19:34 +00:00
|
|
|
int num_students = read_students(students, MAX_STUDENTS);
|
2024-03-21 23:13:59 +00:00
|
|
|
|
2024-03-22 07:30:52 +00:00
|
|
|
// Zoradíme študentov podľa počtu hlasov a mena
|
2024-03-21 23:18:00 +00:00
|
|
|
qsort(students, num_students, sizeof(struct Student), compare);
|
2024-03-21 23:04:41 +00:00
|
|
|
|
2024-03-21 23:18:00 +00:00
|
|
|
// Vypíšeme výsledky
|
|
|
|
printf("Výsledky:\n");
|
2024-03-21 23:04:41 +00:00
|
|
|
for (int i = 0; i < num_students; i++) {
|
2024-03-21 23:18:00 +00:00
|
|
|
printf("%d %s\n", students[i].votes, students[i].name);
|
2024-03-21 22:58:42 +00:00
|
|
|
}
|
2024-03-21 23:01:55 +00:00
|
|
|
|
2024-03-21 22:47:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|