news
This commit is contained in:
parent
d810a971d6
commit
201ccc2d60
63
a3/program.c
63
a3/program.c
@ -0,0 +1,63 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#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;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user