fork download
  1. #include <stdio.h>
  2.  
  3. typedef struct {
  4. int id;
  5. int weight;
  6. int height;
  7. } Body;
  8.  
  9.  
  10. void swap(Body *p1, Body *p2){
  11. int t_id = p1->id;
  12. p1->id = p2->id;
  13. p2->id = t_id;
  14.  
  15. int t_w = p1->weight;
  16. p1->weight = p2->weight;
  17. p2->weight = t_w;
  18.  
  19. int t_h = p1->height;
  20. p1->height = p2->height;
  21. p2->height = t_h;
  22. }
  23.  
  24. int main(void) {
  25. Body a[] = {
  26. {1, 65, 169},
  27. {2, 73, 170},
  28. {3, 59, 161},
  29. {4, 79, 175},
  30. {5, 55, 168},
  31. };
  32.  
  33. int n = sizeof(a) / sizeof(a[0]);
  34. for (int i = 0; i < n - 1; i++){
  35. for (int j = 0; j < n - 1 - i; j++) {
  36. if (a[j].height < a[j + 1].height) {
  37. swap(&a[j], &a[j + 1]);
  38. }
  39. }
  40. }
  41.  
  42. for (int i = 0; i < n; i++) {
  43. printf("Id;%d, 体重;%d, 身長;%d\n", a[i].id, a[i].weight, a[i].height);
  44. }
  45.  
  46. return 0;
  47. }
  48.  
  49.  
Success #stdin #stdout 0.01s 5320KB
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