fork download
  1. #include <stdio.h>
  2.  
  3. // 構造体の定義
  4. typedef struct {
  5. int id; // ID
  6. float weight; // 体重
  7. float height; // 身長
  8. } Body;
  9.  
  10. // 構造体の値を交換するswap関数
  11. void swap(Body *b, Body *c) {
  12. Body temp = *b;
  13. *b = *c;
  14. *c = temp;
  15. }
  16.  
  17. int main() {
  18. // 構造体配列の初期化
  19. Body people[] = {
  20. {1, 65.5, 169.0},
  21. {2, 73.0, 170.0},
  22. {3, 59.0, 161.0},
  23. {4, 79.0, 175.0},
  24. {5, 55.0, 168.0}
  25. };
  26.  
  27. int n = sizeof(people) / sizeof(people[0]);
  28.  
  29. // 身長で降順ソート
  30. for (int i = 0; i < n - 1; i++) {
  31. for (int j = i + 1; j < n; j++) {
  32. if (people[i].height < people[j].height) {
  33. swap(&people[i], &people[j]);
  34. }
  35. }
  36. }
  37.  
  38. // ソート後の出力
  39. printf("ID, Weight, Height\n");
  40. for (int i = 0; i < n; i++) {
  41. printf("%d, %.1f, %.1f\n", people[i].id, people[i].weight, people[i].height);
  42. }
  43.  
  44. return 0;
  45. }
  46.  
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
ID, Weight, Height
4, 79.0, 175.0
2, 73.0, 170.0
1, 65.5, 169.0
5, 55.0, 168.0
3, 59.0, 161.0