What is the Bubble short error in this code?

Asked

Viewed 91 times

1

I tried to solve this problem of URI Online Judge and I came to this code, and it works for test cases smaller than the problem, and I’ve already looked for the bug, only I can’t find it.

Uri 2312

Code:

#include<stdio.h>
#include<stdlib.h>
typedef struct paises{
    unsigned int ouro,prata,bronze;
    int *v;
    char nome[50];
}classe;

int main(){
    classe *v;
    classe ord;
    short int n,i;
    scanf("%hd", &n);
    v = (classe *)malloc(n * sizeof(classe));
    for(i=0;i<n;i++){
        fflush(stdin);
        scanf("%s",v[i].nome);
        scanf("%d",&v[i].ouro);
        scanf("%d",&v[i].prata);
        scanf("%d",&v[i].bronze);
    }
        for(i=0;i<n-1;i++){
            if(v[i].ouro>v[i+1].ouro){
                ord=v[i];
                v[i]=v[i+1];
                v[i+1]=ord;
            }

             if(v[i].prata>v[i+1].prata&&v[i].ouro==v[i+1].ouro){
            ord=v[i];
            v[i]=v[i+1];
            v[i+1]=ord;
            }
             if(v[i].bronze>v[i+1].bronze&&v[i].prata==v[i+1].prata&&v[i].ouro==v[i+1].ouro){
            ord=v[i];
            v[i]=v[i+1];
            v[i+1]=ord;
            }


        }

    for(i=n-1;i>=0;i--){
    printf("\n%s %d %d %d\n",v[i].nome,v[i].ouro,v[i].prata,v[i].bronze);
    }

Exit from the Code:

Resposta

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.