Difference between revisions of "Solução: Problema do professor de terceiro ano (Márcio Trindade)"
From AdonaiMedrado.Pro.Br
Line 1: | Line 1: | ||
<code lang="c"> | <code lang="c"> | ||
#include<stdio.h> | #include<stdio.h> | ||
− | + | #include <stdlib.h> | |
− | + | #include<string.h> | |
+ | |||
int main(void) | int main(void) | ||
{ | { | ||
− | + | struct estudante | |
− | + | { | |
char nome[51]; | char nome[51]; | ||
int resp; | int resp; | ||
int nota; | int nota; | ||
− | + | }; | |
− | + | ||
− | struct | + | int comparar(const void *a, const void *b) |
− | int i,j, gab, n, bin1, acum; | + | { |
+ | return (*(struct estudante *)a).nota-(*(struct estudante *)b).nota; | ||
+ | } | ||
+ | struct estudante aluno[100], troca; | ||
+ | int i,j, gab, n, bin1,quantm=0,minipont=0,maxpont=0, acum; | ||
float med=0; | float med=0; | ||
− | + | scanf("%d",&gab); | |
− | + | ||
scanf("%d",&n); | scanf("%d",&n); | ||
for(i=0;i<n;i++) | for(i=0;i<n;i++) | ||
− | + | { | |
scanf("%s",aluno[i].nome); | scanf("%s",aluno[i].nome); | ||
scanf("%d",&(aluno[i].resp)); | scanf("%d",&(aluno[i].resp)); | ||
− | bin1 = gab&aluno[i].resp; | + | bin1 = gab & aluno[i].resp; |
acum=0; | acum=0; | ||
for(j=0;j<7;j++) | for(j=0;j<7;j++) | ||
− | + | acum += ((bin1>>j)&1); | |
aluno[i].nota = acum; | aluno[i].nota = acum; | ||
− | + | if(maxpont<acum) | |
− | + | maxpont=acum; | |
− | + | med+=acum; | |
− | + | } | |
− | + | qsort(aluno,n,sizeof(struct estudante),comparar); | |
− | + | printf("%d ",aluno[0].nota); | |
− | + | for(i=0;i<=n;i++) | |
− | + | if(minipont==aluno[i].nota) | |
− | + | printf("%s ", aluno[i].nome); | |
− | + | printf("\n%d ", maxpont); | |
− | + | for(i=0;i<=n;i++) | |
− | + | if(aluno[i].nota==maxpont) | |
− | + | {quantm+=1; | |
− | + | printf("%s ",aluno[i].nome); | |
− | + | }printf("\n%.1f", med/n); | |
− | + | printf("\n\n"); | |
− | + | system("PAUSE"); | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
return 0; | return 0; | ||
} | } | ||
− | |||
</code> | </code> |
Revision as of 00:53, 8 June 2009
#include<stdio.h> #include <stdlib.h> #include<string.h> int main(void) { struct estudante { char nome[51]; int resp; int nota; }; int comparar(const void *a, const void *b) { return (*(struct estudante *)a).nota-(*(struct estudante *)b).nota; } struct estudante aluno[100], troca; int i,j, gab, n, bin1,quantm=0,minipont=0,maxpont=0, acum; float med=0; scanf("%d",&gab); scanf("%d",&n); for(i=0;i<n;i++) { scanf("%s",aluno[i].nome); scanf("%d",&(aluno[i].resp)); bin1 = gab & aluno[i].resp; acum=0; for(j=0;j<7;j++) acum += ((bin1>>j)&1); aluno[i].nota = acum; if(maxpont<acum) maxpont=acum; med+=acum; } qsort(aluno,n,sizeof(struct estudante),comparar); printf("%d ",aluno[0].nota); for(i=0;i<=n;i++) if(minipont==aluno[i].nota) printf("%s ", aluno[i].nome); printf("\n%d ", maxpont); for(i=0;i<=n;i++) if(aluno[i].nota==maxpont) {quantm+=1; printf("%s ",aluno[i].nome); }printf("\n%.1f", med/n); printf("\n\n"); system("PAUSE"); return 0; }