#include <stdio.h>
typedef struct {
int id;
int weight;
int height;
} Body;
// データを入れ替えるためのswap関数
void swap(Body *a, Body *b) {
Body temp = *a;
*a = *b;
*b = temp;
}
// 身長を基準に降順ソート
void sortByHeightDescending(Body arr[], int size) {
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (arr[j].height < arr[j + 1].height) {
swap(&arr[j], &arr[j + 1]);
}
}
}
}
int main() {
// データの初期化
Body bodies[] = {
{1, 65, 169},
{2, 73, 170},
{3, 59, 161},
{4, 79, 175},
{5, 55, 168}
};
int size = sizeof(bodies) / sizeof(bodies[0]);
// 身長を基準に降順にソート
sortByHeightDescending(bodies, size);
// 結果を出力
for (int i = 0; i < size; i++) {
printf("%d, %d, %d\n", bodies
[i
].
id, bodies
[i
].
weight, bodies
[i
].
height); }
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+Cgp0eXBlZGVmIHN0cnVjdCB7CiAgICBpbnQgaWQ7CiAgICBpbnQgd2VpZ2h0OwogICAgaW50IGhlaWdodDsKfSBCb2R5OwoKLy8g44OH44O844K/44KS5YWl44KM5pu/44GI44KL44Gf44KB44Guc3dhcOmWouaVsAp2b2lkIHN3YXAoQm9keSAqYSwgQm9keSAqYikgewogICAgQm9keSB0ZW1wID0gKmE7CiAgICAqYSA9ICpiOwogICAgKmIgPSB0ZW1wOwp9CgovLyDouqvplbfjgpLln7rmupbjgavpmY3poIbjgr3jg7zjg4gKdm9pZCBzb3J0QnlIZWlnaHREZXNjZW5kaW5nKEJvZHkgYXJyW10sIGludCBzaXplKSB7CiAgICBmb3IgKGludCBpID0gMDsgaSA8IHNpemUgLSAxOyBpKyspIHsKICAgICAgICBmb3IgKGludCBqID0gMDsgaiA8IHNpemUgLSBpIC0gMTsgaisrKSB7CiAgICAgICAgICAgIGlmIChhcnJbal0uaGVpZ2h0IDwgYXJyW2ogKyAxXS5oZWlnaHQpIHsKICAgICAgICAgICAgICAgIHN3YXAoJmFycltqXSwgJmFycltqICsgMV0pOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQp9CgppbnQgbWFpbigpIHsKICAgIC8vIOODh+ODvOOCv+OBruWIneacn+WMlgogICAgQm9keSBib2RpZXNbXSA9IHsKICAgICAgICB7MSwgNjUsIDE2OX0sCiAgICAgICAgezIsIDczLCAxNzB9LAogICAgICAgIHszLCA1OSwgMTYxfSwKICAgICAgICB7NCwgNzksIDE3NX0sCiAgICAgICAgezUsIDU1LCAxNjh9CiAgICB9OwogICAgaW50IHNpemUgPSBzaXplb2YoYm9kaWVzKSAvIHNpemVvZihib2RpZXNbMF0pOwoKICAgIC8vIOi6q+mVt+OCkuWfuua6luOBq+mZjemghuOBq+OCveODvOODiAogICAgc29ydEJ5SGVpZ2h0RGVzY2VuZGluZyhib2RpZXMsIHNpemUpOwoKICAgIC8vIOe1kOaenOOCkuWHuuWKmwogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBzaXplOyBpKyspIHsKICAgICAgICBwcmludGYoIiVkLCAlZCwgJWRcbiIsIGJvZGllc1tpXS5pZCwgYm9kaWVzW2ldLndlaWdodCwgYm9kaWVzW2ldLmhlaWdodCk7CiAgICB9CgogICAgcmV0dXJuIDA7Cn0K
4, 79, 175
2, 73, 170
1, 65, 169
5, 55, 168
3, 59, 161