From 201ccc2d6070715e245c8a9a7b49c6e0910f8324 Mon Sep 17 00:00:00 2001 From: vafan Date: Thu, 26 Mar 2026 11:02:38 +0000 Subject: [PATCH] news --- a3/program.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/a3/program.c b/a3/program.c index e69de29..2988b07 100644 --- a/a3/program.c +++ b/a3/program.c @@ -0,0 +1,63 @@ +#include +#include +#include + +#define MAX_NAME_LEN 256 +#define MAX_STUDENTS 10000 + +int compare(const void *a, const void *b) { + return strcmp(*(const char **)a, *(const char **)b); +} + +int main() { + int max_students; + if (scanf("%d", &max_students) != 1 || max_students <= 0) { + puts("Nespravny vstup"); + return 1; + } + + char buffer[MAX_NAME_LEN]; + fgets(buffer, sizeof(buffer), stdin); + + char *names[MAX_STUDENTS]; + int counter = 0; + + while (fgets(buffer, sizeof(buffer), stdin)) { + // remove newline + buffer[strcspn(buffer, "\n")] = '\0'; + if (buffer[0] == '\0') break; + + // check duplicate + int duplicate = 0; + for (int i = 0; i < counter; i++) { + if (strcmp(names[i], buffer) == 0) { duplicate = 1; break; } + } + if (!duplicate) { + names[counter++] = strdup(buffer); + if (counter >= MAX_STUDENTS) break; + } + } + + if (counter == 0) { + puts("Ziadne prihlasky"); + return 1; + } + + qsort(names, counter, sizeof(char *), compare); + + puts("Prijati studenti:"); + int accepted = counter < max_students ? counter : max_students; + for (int i = 0; i < accepted; i++) { + puts(names[i]); + } + + if (counter > max_students) { + puts("Neprijati studenti:"); + for (int i = max_students; i < counter; i++) { + puts(names[i]); + } + } + + for (int i = 0; i < counter; i++) free(names[i]); + return 0; +} \ No newline at end of file