pvjc20/du5/program.c

90 lines
2.4 KiB
C
Raw Normal View History

2020-04-07 11:29:11 +00:00
#include <stdio.h>
2020-04-07 14:04:31 +00:00
#include <string.h>
2020-04-07 11:29:11 +00:00
int main(){
2020-04-07 14:04:31 +00:00
char name[50][50];
int score[50];
int pos = 0;
while(1){
2020-04-07 14:15:24 +00:00
scanf("%d %[^\n]", &score[pos], name[pos]);
2020-04-07 14:04:31 +00:00
scanf("%*c");
pos++;
int tmpCh = getc(stdin);
if(tmpCh == '\n') break;
ungetc(tmpCh, stdin);
}
char newName[50][50];
int newScore[50];
int newPos = 0;
for(int i = 0; i < pos; i++){
int check = 0;
for(int q = 0; q < newPos; q++){
if(q != i){
if(strcmp(name[i], newName[q]) == 0){
check = 1;
break;
}
}
}
if(check == 0){
strcpy(newName[newPos], name[i]);
newScore[newPos] = 0;
newPos++;
}
}
for(int i = 0; i < newPos; i++){
for(int q = 0; q < pos; q++){
if(strcmp(newName[i], name[q]) == 0) newScore[i] += score[q];
}
}
for(int i = 0; i < newPos - 1; i++){
if(newScore[i] < newScore[i + 1]){
int tmpScore = newScore[i];
newScore[i] = newScore[i + 1];
newScore[i + 1] = tmpScore;
char tmpName[50];
strcpy(tmpName, newName[i]);
strcpy(newName[i], newName[i + 1]);
strcpy(newName[i + 1], tmpName);
i = 0;
}
else if(newScore[i] == newScore[i + 1]){
int test = 0;
for(int q = 0; q < newPos; q++){
if(newName[i][pos] < newName[i + 1][pos]){
int tmpScore = newScore[i];
newScore[i] = newScore[i + 1];
newScore[i + 1] = tmpScore;
char tmpName[50];
strcpy(tmpName, newName[i]);
strcpy(newName[i], newName[i + 1]);
strcpy(newName[i + 1], tmpName);
break;
}
else{
int tmpScore = newScore[i + 1];
newScore[i + 1] = newScore[i];
newScore[i] = tmpScore;
char tmpName[50];
strcpy(tmpName, newName[i + 1]);
strcpy(newName[i + 1], newName[i]);
strcpy(newName[i], tmpName);
break;
}
}
}
}
2020-04-07 14:27:43 +00:00
printf("Vysledky:\n");
2020-04-07 14:04:31 +00:00
for(int i = 0; i < newPos; i++){
printf("%d %s\n", newScore[i], newName[i]);
}
2020-04-07 11:29:11 +00:00
}