關于降序排列的求助!
#include <stdio.h>
int main()
{
? ? int score[10]={67,98,75,63,82,79,81,91,66,84};
? ??
? ? //總分
? ? int sum=0;
? ? int i;
? ? for(i=0;i<10;i++)
? ? {
? ? ? ? sum=sum+score[i];
? ? }
? ? printf("總分%d\n\n",sum);
? ??
? ? //最高分
? ? int max=0;
? ? int j;
? ? for(j=0;j<9;j++)
? ? {
? ? ? ? if(max<score[j])
? ? ? ? {max=score[j];}
? ? }
? ? printf("最高分%d\n\n",max);
? ??
? ? //最低分
? ? int min=200;
? ? int q;
? ? for(q=0;q<9;q++)
? ? {
? ? ? ? if(min>score[q])
? ? ? ? {min=score[q];}
? ? }
? ? printf("最低分%d\n\n",min);
? ??
? ? //平均分
? ? double avg=sum/10;
? ? printf("平均分%f\n\n",avg);
? ??
? ? //考試成績降序排列
? ? int temp;
? ? int m,n;
? ? printf("#####考試降序排列#####\n\n");
? ? for(m=0;m<9;m++)
? ? if(score[m]<score[m+1])
? ? {
? ? ? ? temp=score[m];
? ? ? ? score[m]=score[m+1];
? ? ? ? score[m+1]=temp;
? ? }
? ? for(n=0;n<10;n++)
? ? printf("%d ",score[n]);
? ??
? ??
? ??
? ??
? ? return 0;
}
-------------------------------------------------------------------------------------------------
各位大神最后這個降序排列,運行結(jié)果是沒有排列之前的,為什么?哪里出錯了額
2015-05-19
printf("#####考試降序排列#####\n\n");
? ?for(n=8;n>=0;n--)
? ? {
? ? for(n=0;m<9;n++)
? ? if(score[m]<score[m+1])
? ? {
? ? ? ? temp=score[m];
? ? ? ? score[m]=score[m+1];
? ? ? ? score[m+1]=temp;
? ? }
? ?}
可以改成這樣..............................
2015-05-16
?改成這樣就可以了//考試成績降序排列
?
??? int temp;
??? int m,n;
??? printf("#####考試降序排列#####\n\n");
??? for(m=0;m<9;m++)
??for(n=m+1;n<10;n++)
??? if(score[m]<score[n])
??? {
??????? temp=score[m];
??????? score[m]=score[n];
??????? score[n]=temp;
??? }
2015-05-06
是全局變量什么的嗎?我不太懂。。