fork download
  1. #include <stdio.h>
  2.  
  3. //構造体の定義
  4. typedef struct {
  5. int id;
  6. int weight;
  7. int height;
  8. } Body;
  9.  
  10. //swap関数を用いて中身を入れ替える
  11. void swap(Body*x,Body*y) {
  12. Body temp=*x;
  13. *x=*y;
  14. *y=temp;
  15. }
  16.  
  17. //配列の初期化
  18. int main() {
  19. Body a[]={
  20. {1, 65, 169},
  21. {2, 73, 170},
  22. {3, 59, 161},
  23. {4, 79, 175},
  24. {5, 55, 168}
  25. };
  26. int n=sizeof(a)/sizeof(a[0]); //データの数が変わっても書き直さなくて良いようにする
  27.  
  28. for(int i=0;i<n-1;i++) {
  29. for (int j=0;j<n-1-i;j++) {
  30. if (a[j].height<a[j+1].height) {
  31. swap(&a[j],&a[j+1]);
  32. }
  33. }
  34. }
  35.  
  36. for(int i=0;i<n;i++) {
  37. printf("%d, %d, %d\n",a[i].id,a[i].weight,a[i].height);
  38. }
  39.  
  40. return 0;
  41. }
Success #stdin #stdout 0s 5316KB
stdin
Standard input is empty
stdout
4, 79, 175
2, 73, 170
1, 65, 169
5, 55, 168
3, 59, 161