为什么比较的顺序不同结果不同???
#include<stdio.h>struct student
{
int number;
char name[7];
float score;
};
int main()
{
struct student stu[5] = { {01,"GY",97},{02,"Yoo",87},{03,"Gong",87.7},{04,"shacha",97.7},{05,"erzi",77} };
struct student t;
int i, j, k;
for (i = 3; i >=0; i--) //
{
k = i; 这
for (j = i + 1; j >0; j--)
{
if (stu[k].score < stu[j].score) 部
k = j;
t = stu[i]; stu[i] = stu[k]; stu[k] = t; 分
}
} //
for (i = 0; i < 5; i++)
printf("%6d %8s %6.2f\n", stu[i].number, stu[i].name, stu[i].score);
return 0;
}
而下面这样就不同了
for (i = 0; i <4; i++)
{
k = i;
for (j = i + 1; j <5; j++)
{
if (stu[k].score < stu[j].score)
k = j;
t = stu[i]; stu[i] = stu[k]; stu[k] = t;
}
}
或者以下两种,为什么结果不同???
for (i = 0; i <4; i++)
{
k = i;
for (j = i + 1; j <5; j++)
{
if (stu[j].score >stu[k].score)
k = j;
t = stu[i]; stu[i] = stu[k]; stu[k] = t;
}
}
for (i = 0; i <4; i++)
{
k = i;
for (j = i + 1; j <5; j++)
{
if (stu[k].score < stu[j].score)
k = j;
t = stu[i]; stu[i] = stu[k]; stu[k] = t;
}
}