#include <stdio.h>
int main()
{
int score[]={67,98,75,63,82,79,81,91,66,84};
int i,j;
int temp;
for (i=9;i>=0;i--)
{
for (j=0;j<9;j++)
{
if (score[i]<score[j])
{
temp=score[i];
score[i]=score[j];
score[j]=temp;
}
}
}
int sum=0;
int k;
for (k=0;k<10;k++)
{
sum=sum+score[k];
printf("%d ",score[k]);
}
printf("\nTotal score=%d\n",sum);
printf("Maximum score=%d\n",score[k-1]);
printf("Minimum score=%d\n",score[0]);
printf("Average score=%d\n",sum/10);
return 0;
}
结果 91 63 66 67 75 79 81 82 84 98 Total score=786 Maximum score=98 Minimum score=91 Average score=78
你这个不是冒泡排序,从if开始就错了
if (score[i]<score[j])
{
temp=score[i];
score[i]=score[j];
score[j]=temp;
}
应该是score[j]和score[j+1]进行比较吧。
for(i=9; i>=0; i--)
{
for(j=0;j<=i;j++)
{
if(score[j]<score[j+1]) //当前面的数比后面的数大时
{
int temp; //定义临时变量temp
temp=score[j]; //将前面的数赋值给temp
score[j]=score[j+1]; //前后之数颠倒位置
arr[j+1]=temp; //将较大的数放在后面
}
}
}
#define N 10
for(i = N - 1; i > 0; i--)
{
for(j = 0; j <= i; j++)
{
if(score[j] < score[j+1])
{
temp = score[j];
score[j] = score[j+1];
score[j+1] = temp;
}
}
}
for (i=8;i>=0;i--)
{
for (j=0;j<=i;j++)
{
if (score[j]<score[j+1])
{
temp=score[j];
score[j]=score[j+1];
score[j+1]=temp;
}
}
}