fork download
  1. #include <stdio.h>
  2.  
  3. typedef struct {
  4. int id;
  5. int weight;
  6. int height;
  7. } Body;
  8.  
  9. void swap(Body *x, Body *y) {
  10. Body w = *x;
  11. *x = *y;
  12. *y = w;
  13. }
  14.  
  15. void sort(Body data[], int n) {
  16. for(int i = 0; i < n - 1; i++) {
  17. for(int j = 0; j < n - 1 ; j++) {
  18. if(data[j].height < data[j + 1].height) {
  19. swap(&data[j], &data[j + 1]);
  20. }
  21. }
  22. }
  23. }
  24.  
  25. int main() {
  26. Body data[] = {
  27. {1,65,169},
  28. {2,73,170},
  29. {3,59,161},
  30. {4,79,175},
  31. {5,55,168}
  32. };
  33.  
  34. int n = 5;
  35.  
  36. sort(data, n);
  37.  
  38. for(int i = 0; i < n; i++) {
  39. printf("ID=%d, 体重=%d, 身長=%d\n",
  40. data[i].id, data[i].weight, data[i].height);
  41. }
  42.  
  43. return 0;
  44. }
  45.  
Success #stdin #stdout 0s 5316KB
stdin
Standard input is empty
stdout
ID=4, 体重=79, 身長=175
ID=2, 体重=73, 身長=170
ID=1, 体重=65, 身長=169
ID=5, 体重=55, 身長=168
ID=3, 体重=59, 身長=161