This commit is contained in:
Oleksandr Vyshniakov 2025-03-20 12:36:20 +01:00
parent 078b4436d6
commit ad8f78db53
6 changed files with 60 additions and 27 deletions

Binary file not shown.

View File

@ -14,10 +14,10 @@ int main() {
count++; //нумерация чисел
}
if (count == 0) { //если не было введено никакое число
printf("Chyba: Malo platnych hodnot.\n");
return 0;
}
if (count == 0) { //если не было введено никакое число
printf("Chyba: Malo platnych hodnot.\n");
return 0;
}
for (int i = 0; i < count; i++) { //выписывание всех чисел
printf("Sutaziaci c. %d vypil %d poharov.\n",i+1,results[i]);

Binary file not shown.

Binary file not shown.

View File

@ -1,36 +1,69 @@
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LENGTH 100
typedef struct {
char name[100];
int golosa;
} nameandgolosa;
char name[MAX_NAME_LENGTH];
int votes;
} Student;
#define MAX_STUDENTS 1000;
int compare_students(const void *a, const void *b) {
const Student *studentA = (const Student *)a;
const Student *studentB = (const Student *)b;
int najtistudenta (nameandgolosa students[], int count, const char* name) {
for (int i = 0; i < count; i++) {
if (strcmp(students[i].name, name) == 0) {
return i;
}
if (studentA->votes != studentB->votes) {
return studentB->votes - studentA->votes; // Сортировка по убыванию голосов
} else {
return strcmp(studentA->name, studentB->name); // Лексикографическая сортировка по имени
}
return -1;
}
int compare (const void* a, const void* b) {
nameandgolosa* studentA = (nameandgolosa*)a;
nameandgolosa* studentB = (nameandgolosa*)b;
if (studentA->golosa != studentB->golosa) {
return studentB->golosa - studentA->golosa;
}
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;
}

Binary file not shown.