Difference between revisions of "Solução: Problema do baile de casais (Adriano Queiroz)"

From AdonaiMedrado.Pro.Br
Jump to: navigation, search
(New page: <code lang = "c"> #include <stdio.h> #include <stdlib.h> int comparar(const void *a, const void *b){ return *(int *)a-*(int *)b; } int main(void){ int convidados, qtdHomens=0, qtdMu...)
 
 
(One intermediate revision by one other user not shown)
Line 34: Line 34:
 
     else {
 
     else {
 
         qsort(mulheres,qtdMulheres ,sizeof(int), comparar);
 
         qsort(mulheres,qtdMulheres ,sizeof(int), comparar);
      qsort(homens,qtdHomens ,sizeof(int), comparar);
+
        qsort(homens,qtdHomens ,sizeof(int), comparar);
 
         do{
 
         do{
 
             if (mulheres[j] < homens[l++])
 
             if (mulheres[j] < homens[l++])
Line 44: Line 44:
 
             printf("F");
 
             printf("F");
 
     }
 
     }
 +
    free(homens);
 +
    free(mulheres);
 +
    return 0;
 
}
 
}
 
</code>
 
</code>

Latest revision as of 14:17, 6 June 2009

#include <stdio.h>
#include <stdlib.h>
 
int comparar(const void *a, const void *b){
	return *(int *)a-*(int *)b;
}
 
int main(void){
    int convidados, qtdHomens=0, qtdMulheres=0, *homens, *mulheres;
    int i, j=0, l=0, idade;
 
    scanf("%d", &convidados);
    homens = (int*) malloc(convidados*sizeof(int));
    mulheres = (int*) malloc(convidados*sizeof(int));
 
    for (i=0; i<convidados; i++){
        scanf("%d", &idade);
        if (idade%2)
            homens[qtdHomens++]=idade;
        else
            mulheres[qtdMulheres++]=idade;
    }
 
    if (!(qtdHomens)){
        if (qtdMulheres)
            printf("F");
        else
            printf("S");
    }
    else if (!(qtdMulheres))
        printf("S");
 
    else {
        qsort(mulheres,qtdMulheres ,sizeof(int), comparar);
        qsort(homens,qtdHomens ,sizeof(int), comparar);
        do{
            if (mulheres[j] < homens[l++])
                j++;
        }while(j<qtdMulheres && l<qtdHomens);
        if (j==qtdMulheres)
            printf("S");
        else
            printf("F");
    }
    free(homens);
    free(mulheres);
    return 0;
}