1
I made the point Who’s Going to Fail? that had the rule Students would be ordered according to the number of problems solved, with draws solved according to the alphabetical order of the names (there are no homonyms in the class), Well I managed to solve only that I wanted another way because in that needed to order twice My code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct pessoas
{
char nome[100];
int nota;
};
struct pessoas pessoa[102];
void ordena(int teste);
void ordena2(int teste);
int main()
{
int teste, i, cont = 0;
while(scanf("%d", &teste) == 1)
{
for(i = 0; i < teste; i++)
{
scanf("%s %d", pessoa[i].nome, &pessoa[i].nota);
}
ordena(teste);
ordena2(teste);
printf("Instancia %d\n", ++cont);
puts(pessoa[teste - 1].nome);
putchar('\n');
}
system("pause");
return 0;
}
void ordena(int teste)
{
int i, j , aux;
char a[100];
for(i = 0; i < teste; i++)
{
for(j = 0; j < teste; j++)
{
if(pessoa[i].nota > pessoa[j].nota)
{
aux = pessoa[i].nota;
pessoa[i].nota = pessoa[j].nota;
pessoa[j].nota = aux;
strcpy(a, pessoa[i].nome);
strcpy(pessoa[i].nome, pessoa[j].nome);
strcpy(pessoa[j].nome, a);
}
}
}
}
void ordena2(int teste)
{
int i, j, aux;
char a[100];
for(i = 0; i < teste; i++)
{
for(j = i + 1; j < teste; j++)
{
if(pessoa[i].nota == pessoa[j].nota)
{
if(strcmp(pessoa[i].nome, pessoa[j].nome) > 0)
{
strcpy(a, pessoa[i].nome);
strcpy(pessoa[i].nome, pessoa[j].nome);
strcpy(pessoa[j].nome, a);
aux = pessoa[i].nota;
pessoa[i].nota = pessoa[j].nota;
pessoa[j].nota = aux;
}
}
}
}
}
I didn’t understand very well, want other ways to sort? Like for example Quicksort?
– Fábio Morais
Could be, the problem I can’t sort out with the rules as the question asks
– rafael marques
I don’t quite understand, but instead of doing the separate functions you can put them together
– Fábio Morais
It would have a more summarized way of doing this, because it has several if and slows down time
– rafael marques
Of course, but this has to be seen in other ways, its ordering method is O(N 2), I will refer in my answer to other ordering methods
– Fábio Morais