Skip to content

Instantly share code, notes, and snippets.

@messense
Created October 14, 2012 10:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save messense/3888252 to your computer and use it in GitHub Desktop.
Save messense/3888252 to your computer and use it in GitHub Desktop.
冒泡排序
/*!
* 某学校有12名学生参加100米短跑比赛,每个运动员号和成绩如4-4所示,
* 请按照比赛成绩排名并输出,要求每一行输出名次、运动员号和比赛成绩三项数据。
* 表4-4 100米短跑比赛成绩
* 运动员号 成绩(秒 运动员号 成绩(秒)
* 001 13.6 031 14.9
* 002 14.8 036 12.6
* 010 12.0 037 13.4
* 011 12.7 102 12.5
* 023 15.6 325 15.3
* 025 13.4 438 12.7
*/
#include <stdio.h>
// 冒泡排序成绩
void sort_score(float score[12][2]) {
int i, j;
float tmp[2];
for(i = 0; i < 12; i++) {
for(j = 0; j < 11-i; j++) {
if(score[j][1] > score[j+1][1]) {
tmp[0] = score[j][0];
tmp[1] = score[j][1];
score[j][0] = score[j+1][0];
score[j][1] = score[j+1][1];
score[j+1][0] = tmp[0];
score[j+1][1] = tmp[1];
}
}
}
}
int main(int argc, char const *argv[])
{
float score[12][2] = {
{001, 13.6},
{002, 14.8},
{010, 12.0},
{011, 12.7},
{023, 15.6},
{025, 13.4},
{031, 14.9},
{036, 12.6},
{037, 13.4},
{102, 12.5},
{325, 15.3},
{438, 12.7}
};
sort_score(score);
int i;
printf("名次 编号 成绩\n");
for(i = 0; i < 12; i++) {
printf("%2d %03d %.1f\n", i+1, (int)score[i][0], score[i][1]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment