At the city informatics Olympiad, participants were asked to complete 3 tasks, each of which was rated on a 25-point scale. It is known that the total number of participants in the first round of the Olympiad does not exceed 250 people. At the entrance of the program are given information about the results of the Olympiad. The first line introduces the number of participants N. Next are N lines with the following format:

<Last Name> <Name> <Points>

Here, <Last Name> is a string consisting of no more than 20 characters; <Name> is a string consisting of no more than 15 characters; <Points> is a string containing three integers, separated by a space, corresponding to the points received by the participant for each task of the first round. In this case, <Last Name> and <Name>, <Name> and <Points> are separated by a single space. Examples of input lines:

Petrova Olga 25 18 16

Kalinichenko Ivan 14 19 15

Write a program that will display the name and surname of the participant who has scored the maximum number of points. If among the other participants there are students who have scored the same number of points, then their names and surnames should also be derived. In this case, the names and surnames can be displayed in any order.

Here is what I came up with:

#include <stdio.h> #include <math.h> #include <string.h> main(){ int dig[100];/*счетчик*/ char fam[19]; char name[14]; int i,j,n,c,st,rez1,rez2,rez3,rezmax,rez; printf("Кол-воо участников :\n"); scanf("%d",n); for (i = 0; i < n; i++ ){ dig[i] = 0;/*Все элементы счетчика равны нулю*/ } printf("\nВводите результаты:\n"); rezmax = 0; for (i = 0; i < n; i++ ){ scanf("%s %s %d %d %d",&fam,&name,&rez1,&rez2,&rez3); rez = rez1 + rez2 + rez3; dig[i] = rez; if(rez > rezmax){ rezmax = rez; } } } 

Began to understand, I write:

 #include <stdio.h> #include <math.h> #include <string.h> main(){ struct Rez { char fam[20]; char name[15]; int rez; } ; int rez1; int rez2; int rez3; struct Rez dig[100]; int i,j,n,tmp; printf("Введите число участников: \n"); scanf("%d",&n); printf("Введите имена и результаты участников: \n"); for (i = 0; i < n - 1; i++ ){ scanf("%s %s %d %d %d",&Rez.fam,&Rez.name,&rez1,&rez3,&rez3); Rez.rez = rez1 + rez2 + rez3; dig[i] = Rez; } for (i = n - 1; i >= 1; i-- ){ for (j = 0; j <= i; j++ ){ if (dig[i].rez > dig[j].rez){ tmp = dig[j]; dig[i] = dig[j]; dig[j] = tmp; } } } printf("\n%s %s %d",dig[n - 1].fam,dig[n - 1].name,dig[n - 1].rez); return 0; } 

And to me in response:

 ошибка: unknown type name «Rez» 

with all that it implies. I repeat, I have about an hour of experience with textures. Where have I been wrong? Thank.

Corrected, now:

 c2.c:22:29: ошибка: «Rez» undeclared (first use in this function) c2.c:22:29: замечание: each undeclared identifier is reported only once for each function it appears in c2.c:30:13: ошибка: incompatible types when assigning to type «int» from type «struct Rez» c2.c:32:16: ошибка: incompatible types when assigning to type «struct Rez» from type «int» 

Where am I wrong?

Problems solved. Sorting an array of structures. , the topic can be closed.

  • Create a structure for storing member data. Copy strings using strcpy. Put all such structures in one array, sort by descending points. What is it like? without structures it is desirable - rolton
  • four
    And why did you choose C to take the exam? As far as I know, the exam tasks can be solved in almost any language. But the probability of zafeil on C is very large. - dzhioev
  • Not if, of course, you don’t like using structures, you can create several parallel arrays that will store all the data that would fit comfortably in the fields of the structure. About rationality and the logic of this method already judge. - AseN
  • @ 0xFFh, then at the same time, we would advise the vehicle to switch to Fortran. - avp

2 answers 2

@rolton , without structures there will be serious problems with several participants who have scored the maximum score.

And reading everything and sorting is "unsportsmanlike." The problem is solved in one pass through the file, only "maximalists" accumulate in the memory.

Only for this you will have to familiarize yourself with such a topic as lists. (however, there is no problem; questions about them at the HC were unmeasured).

  • And if you look at the above, where am I wrong and how best to be? - rolton
  • Indeed, the problem statement states that the first line contains N - the number of participants. But you are participating in the Olympiad , so jump higher than the set level. Solve the problem for an arbitrary number of participants. All you need to do is to be able to determine the end of the file (ie, there is no more data) and familiarity with dynamic memory allocation (see man malloc ), and therefore with pointers . - Concerning mistakes: the name Rez is not announced anywhere. Apparently you need struct Rez {...} Rez; tmp db be of type struct rez, not int. In scanf before Rez ... & not needed. etc. - avp

Create a structure for storing member data. Copy strings using strcpy. Put all such structures in one array, sort by descending points. Print the names and surnames of all participants who have as many as the first (including the first). PROFIT!

By the way, there are not enough elements in fam and name - it should be 20 + 1 and 15 + 1, respectively.