fork(1) 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 *a, Body *b) {
  11. Body temp = *a;
  12. *a = *b;
  13. *b = temp;
  14. }
  15.  
  16. int main() {
  17.  
  18. Body a[] = {
  19. {1, 65, 169},
  20. {2, 73, 170},
  21. {3, 59, 161},
  22. {4, 79, 175},
  23. {5, 55, 168}
  24. };
  25.  
  26. int n = sizeof(a) / sizeof(a[0]);
  27.  
  28.  
  29. for (int i = 0; i < n - 1; i++) {
  30. for (int j = 0; j < n - i - 1; j++) {
  31. if (a[j].height < a[j + 1].height) {
  32. swap(&a[j], &a[j + 1]);
  33. }
  34. }
  35. }
  36.  
  37.  
  38. printf("ID 体重 身長\n");
  39. for (int i = 0; i < n; i++) {
  40. printf("%d, %d, %d\n", a[i].id, a[i].weight, a[i].height);
  41. }
  42.  
  43. return 0;
  44. }
  45.  
  46.  
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
ID  体重  身長
4,  79,  175
2,  73,  170
1,  65,  169
5,  55,  168
3,  59,  161