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