#include <stdio.h>
// 構造体の定義
typedef struct {
int id; // ID
float weight; // 体重
float height; // 身長
} Body;
// 構造体の値を交換するswap関数
void swap(Body *b, Body *c) {
Body temp = *b;
*b = *c;
*c = temp;
}
int main() {
// 構造体配列の初期化
Body people[] = {
{1, 65.5, 169.0},
{2, 73.0, 170.0},
{3, 59.0, 161.0},
{4, 79.0, 175.0},
{5, 55.0, 168.0}
};
int n = sizeof(people) / sizeof(people[0]);
// 身長で降順ソート
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (people[i].height < people[j].height) {
swap(&people[i], &people[j]);
}
}
}
// ソート後の出力
printf("ID, Weight, Height\n"); for (int i = 0; i < n; i++) {
printf("%d, %.1f, %.1f\n", people
[i
].
id, people
[i
].
weight, people
[i
].
height); }
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CgovLyDmp4vpgKDkvZPjga7lrprnvqkKdHlwZWRlZiBzdHJ1Y3QgewogICAgaW50IGlkOyAgICAgICAvLyBJRAogICAgZmxvYXQgd2VpZ2h0OyAvLyDkvZPph40KICAgIGZsb2F0IGhlaWdodDsgLy8g6Lqr6ZW3Cn0gQm9keTsKCi8vIOani+mAoOS9k+OBruWApOOCkuS6pOaPm+OBmeOCi3N3YXDplqLmlbAKdm9pZCBzd2FwKEJvZHkgKmIsIEJvZHkgKmMpIHsKICAgIEJvZHkgdGVtcCA9ICpiOwogICAgKmIgPSAqYzsKICAgICpjID0gdGVtcDsKfQoKaW50IG1haW4oKSB7CiAgICAvLyDmp4vpgKDkvZPphY3liJfjga7liJ3mnJ/ljJYKICAgIEJvZHkgcGVvcGxlW10gPSB7CiAgICAgICAgezEsIDY1LjUsIDE2OS4wfSwKICAgICAgICB7MiwgNzMuMCwgMTcwLjB9LAogICAgICAgIHszLCA1OS4wLCAxNjEuMH0sCiAgICAgICAgezQsIDc5LjAsIDE3NS4wfSwKICAgICAgICB7NSwgNTUuMCwgMTY4LjB9CiAgICB9OwoKICAgIGludCBuID0gc2l6ZW9mKHBlb3BsZSkgLyBzaXplb2YocGVvcGxlWzBdKTsKCiAgICAvLyDouqvplbfjgafpmY3poIbjgr3jg7zjg4gKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbiAtIDE7IGkrKykgewogICAgICAgIGZvciAoaW50IGogPSBpICsgMTsgaiA8IG47IGorKykgewogICAgICAgICAgICBpZiAocGVvcGxlW2ldLmhlaWdodCA8IHBlb3BsZVtqXS5oZWlnaHQpIHsKICAgICAgICAgICAgICAgIHN3YXAoJnBlb3BsZVtpXSwgJnBlb3BsZVtqXSk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CgogICAgLy8g44K944O844OI5b6M44Gu5Ye65YqbCiAgICBwcmludGYoIklELCBXZWlnaHQsIEhlaWdodFxuIik7CiAgICBmb3IgKGludCBpID0gMDsgaSA8IG47IGkrKykgewogICAgICAgIHByaW50ZigiJWQsICUuMWYsICUuMWZcbiIsIHBlb3BsZVtpXS5pZCwgcGVvcGxlW2ldLndlaWdodCwgcGVvcGxlW2ldLmhlaWdodCk7CiAgICB9CgogICAgcmV0dXJuIDA7Cn0K
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