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