fork(1) download
  1. #include <stdio.h>
  2.  
  3. typedef struct {
  4. int id;
  5. int weight;
  6. int height;
  7. } Body;
  8.  
  9. // swap関数: Body型の2つの変数を入れ替える
  10. void swap(Body *a, Body *b) {
  11. Body w = *a;
  12. *a = *b;
  13. *b = w;
  14. }
  15.  
  16. int main() {
  17. Body a[] = {
  18. {1, 65, 169},
  19. {2, 73, 170},
  20. {3, 59, 161},
  21. {4, 79, 175},
  22. {5, 55, 168}
  23. };
  24. int n = sizeof(a) / sizeof(a[0]);
  25.  
  26. // 身長を基準に降順にソート
  27. for (int i = 0; i < n - 1; i++) {
  28. for (int j = 0; j < n - i - 1; j++) {
  29. if (a[j].height < a[j + 1].height) {
  30. swap(&a[j], &a[j + 1]);
  31. }
  32. }
  33. }
  34.  
  35. // 結果の表示
  36. printf("ID\tWeight\tHeight\n");
  37. for (int i = 0; i < n; i++) {
  38. printf("%d\t%d\t%d\n", a[i].id, a[i].weight, a[i].height);
  39. }
  40.  
  41. return 0;
  42. }
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
ID	Weight	Height
4	79	175
2	73	170
1	65	169
5	55	168
3	59	161