#include <stdio.h>
#define N 30
typedef struct seito
{
int num;
char lname[ 20 ] ;
char fname[ 20 ] ;
int point;
} seito;
int main( void )
{
seito d[ N] , t;
char buf[ 100 ] ;
FILE
* fp
= fopen ( "seito.dat" , "r" ) ; int cnt = 0 , i = 0 , j, cmpCnt = 0 ;
while ( fgets ( buf
, 100 , fp
) != NULL
) {
sscanf ( buf
, "%d %s %s %d" , & d
[ i
] .
num , d
[ i
] .
lname , d
[ i
] .
fname , & d
[ i
] .
point ) ; i++;
}
cnt = i;
for ( i = 1 ; i < cnt; i++ )
{
t = d[ i] ;
cmpCnt++;
if ( d[ i - 1 ] .point < t.point )
{
j = i;
do
{
d[ j] = d[ j - 1 ] ;
j--;
cmpCnt++;
}
while ( j > 0 && d[ j - 1 ] .point < t.point ) ;
d[ j] = t;
}
}
for ( int i = 0 ; i < cnt; ++ i)
{
printf ( "%4d %-10s %-10s %4d\n " , d
[ i
] .
num , d
[ i
] .
lname , d
[ i
] .
fname , d
[ i
] .
point ) ; }
printf ( "整列の為の比較回数=%d回\n " , cmpCnt
) ; return 0 ;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNkZWZpbmUgTiAzMAp0eXBlZGVmIHN0cnVjdCBzZWl0bwp7CiAgICBpbnQgbnVtOwogICAgY2hhciBsbmFtZVsyMF07CiAgICBjaGFyIGZuYW1lWzIwXTsKICAgIGludCBwb2ludDsKfSBzZWl0bzsKCgppbnQgbWFpbih2b2lkKQp7CiAgICBzZWl0byBkW05dLCB0OwogICAgY2hhciBidWZbMTAwXTsKICAgIEZJTEUgKmZwID0gZm9wZW4oInNlaXRvLmRhdCIsICJyIik7CiAgICBpbnQgY250ID0gMCwgaSA9IDAsIGosIGNtcENudCA9IDA7CiAgICB3aGlsZSAoZmdldHMoYnVmLCAxMDAsIGZwKSAhPSBOVUxMKQogICAgewogICAgICAgIHNzY2FuZihidWYsICIlZCAlcyAlcyAlZCIsICZkW2ldLm51bSwgZFtpXS5sbmFtZSwgZFtpXS5mbmFtZSwgJmRbaV0ucG9pbnQpOwogICAgICAgIGkrKzsKICAgIH0KICAgIGNudCA9IGk7CiAgICBmb3IgKGkgPSAxOyBpIDwgY250OyBpKyspCiAgICB7CiAgICAgICAgdCA9IGRbaV07CiAgICAgICAgY21wQ250Kys7CiAgICAgICAgaWYgKGRbaSAtIDFdLnBvaW50IDwgdC5wb2ludCkKICAgICAgICB7CiAgICAgICAgICAgIGogPSBpOwogICAgICAgICAgICBkbwogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBkW2pdID0gZFtqIC0gMV07CiAgICAgICAgICAgICAgICBqLS07CiAgICAgICAgICAgICAgICBjbXBDbnQrKzsKICAgICAgICAgICAgfQogICAgICAgICAgICB3aGlsZSAoaiA+IDAgJiYgZFtqIC0gMV0ucG9pbnQgPCB0LnBvaW50KTsKICAgICAgICAgICAgZFtqXSA9IHQ7CiAgICAgICAgfQogICAgfQogICAgcHJpbnRmKCLnlarlj7cgIOawj+WQjSAgICAgICAgICAgICAgICAgICDlvpfngrlcbiIpOwogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBjbnQ7ICsraSkKICAgIHsKICAgICAgICBwcmludGYoIiU0ZCAgJS0xMHMgJS0xMHMgJTRkXG4iLCBkW2ldLm51bSwgZFtpXS5sbmFtZSwgZFtpXS5mbmFtZSwgZFtpXS5wb2ludCk7CiAgICB9CiAgICBwcmludGYoIuaVtOWIl+OBrueCuuOBruavlOi8g+WbnuaVsD0lZOWbnlxuIiwgY21wQ250KTsKICAgIHJldHVybiAwOwp9Cg==