#include <stdio.h>
typedef struct {
int id;
int weight;
int height;
} Body;
// swap関数
void swap(Body *a, Body *b) {
Body w = *a;
*a = *b;
*b = w;
}
// 配列を降順にソートする関数
void sortByHeightDesc(Body a[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (a[j].height < a[j + 1].height) {
swap(&a[j], &a[j + 1]);
}
}
}
}
int main() {
// 構造体配列を初期化
Body a[] = {
{1, 65, 169},
{2, 73, 170},
{3, 59, 161},
{4, 79, 175},
{5, 55, 168}
};
int n = sizeof(a) / sizeof(a[0]);
// 身長の降順にソート
sortByHeightDesc(a, n);
// 結果を表示
for (int i = 0; i < n; i++) {
printf("%d, %d, %d\n", a
[i
].
id, a
[i
].
weight, a
[i
].
height); }
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+Cgp0eXBlZGVmIHN0cnVjdCB7CiAgICBpbnQgaWQ7CiAgICBpbnQgd2VpZ2h0OwogICAgaW50IGhlaWdodDsKfSBCb2R5OwoKLy8gc3dhcOmWouaVsAp2b2lkIHN3YXAoQm9keSAqYSwgQm9keSAqYikgewogICAgQm9keSB3ID0gKmE7CiAgICAqYSA9ICpiOwogICAgKmIgPSB3Owp9CgovLyDphY3liJfjgpLpmY3poIbjgavjgr3jg7zjg4jjgZnjgovplqLmlbAKdm9pZCBzb3J0QnlIZWlnaHREZXNjKEJvZHkgYVtdLCBpbnQgbikgewogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuIC0gMTsgaSsrKSB7CiAgICAgICAgZm9yIChpbnQgaiA9IDA7IGogPCBuIC0gaSAtIDE7IGorKykgewogICAgICAgICAgICBpZiAoYVtqXS5oZWlnaHQgPCBhW2ogKyAxXS5oZWlnaHQpIHsKICAgICAgICAgICAgICAgIHN3YXAoJmFbal0sICZhW2ogKyAxXSk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9Cn0KCmludCBtYWluKCkgewogICAgLy8g5qeL6YCg5L2T6YWN5YiX44KS5Yid5pyf5YyWCiAgICBCb2R5IGFbXSA9IHsKICAgICAgICB7MSwgNjUsIDE2OX0sCiAgICAgICAgezIsIDczLCAxNzB9LAogICAgICAgIHszLCA1OSwgMTYxfSwKICAgICAgICB7NCwgNzksIDE3NX0sCiAgICAgICAgezUsIDU1LCAxNjh9CiAgICB9OwoKICAgIGludCBuID0gc2l6ZW9mKGEpIC8gc2l6ZW9mKGFbMF0pOwoKICAgIC8vIOi6q+mVt+OBrumZjemghuOBq+OCveODvOODiAogICAgc29ydEJ5SGVpZ2h0RGVzYyhhLCBuKTsKCiAgICAvLyDntZDmnpzjgpLooajnpLoKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgcHJpbnRmKCIlZCwgJWQsICVkXG4iLCBhW2ldLmlkLCBhW2ldLndlaWdodCwgYVtpXS5oZWlnaHQpOwogICAgfQoKICAgIHJldHVybiAwOwp9Cg==
4, 79, 175
2, 73, 170
1, 65, 169
5, 55, 168
3, 59, 161