6-12的練習(xí)僅供參考
#include <stdio.h>
#define N 10
int main()
{
? ? int score[N]={67,98,75,63,82,79,81,91,66,84};
? ? int i,j;
? ? int Max=0,Min=score[0],temp;
? ? float Mid=0,Sum=0;
? ? for(i=0;i<N;i++)Sum+=score[i]; //總成績(jī)
? ? Mid=Sum/N;//平均分
? ? for(i=0;i<N;i++)
? ? {
? ? ? ? if(score[i]>Max)Max=score[i]; //最高
? ? ? ? if(score[i]<Min)Min=score[i]; //最低
? ? }
? ? for(i=N-1-1;i>=0;i--)//冒泡排序N-下標(biāo)-1
? ? {
? ? ? ? for(j=0;j<=i;j++)//前一個(gè)與后一個(gè)比較,直到最后
? ? ? ? {
? ? ? ? ? ? if(score[j]<score[j+1])//前一個(gè)小于后一個(gè)則進(jìn)行交換
? ? ? ? ? ? {
? ? ? ? ? ? ? ? temp=score[j];
? ? ? ? ? ? ? ? score[j]=score[j+1];
? ? ? ? ? ? ? ? score[j+1]=temp;
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? printf("總分:%.1f分\n最高分:%d分\n最低分:%d分\n平均分:%.1f分\n",Sum,Max,Min,Mid);
? ? printf("冒泡法降序排序:");
? ? for(i=0;i<N;i++)
? ? ? ? printf("%d ",score[i]);
? ? ? ??
? ? return 0;
}