|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <stdio.h>
struct Student
{
int num;
char name[20];
float score;
}; struct Student stu[10];
int main()
{
int i,j,k;struct Student temp;
for(i=0;i<10;i++)
{scanf("%d,%s,%f",&stu[i].num,&stu[i].name,&stu[i].score);}
for(i=0;i<9;i++)
{k=i;
for(j=i+1;j<10;j++)
if(stu[j].score>stu[k].score)
k=j;
temp=stu[k];stu[k]=stu[i];stu[i]=temp;
}
for(i=0;i<10;i++)
printf("%6d %8s %6.2f\n",stu[i].num,stu[i].name,stu[i].score);
return 0;
}
求问这个代码怎么行不通
- #include <stdio.h>
- struct Student
- {
- int num ;
- char name[20] ;
- float score ;
- } stu[10] ;
- int main(void)
- {
- int i , j , k ; struct Student temp ;
- for(i = 0 ; i < 10 ; i ++) scanf("%d%s%f" , & stu[i] . num , stu[i] . name , & stu[i] . score) ;
- for(i = 0 ; i < 9 ; i ++) {
- for(k = i , j = i + 1 ; j < 10 ; j ++) {
- if(stu[j] . score > stu[k] . score) {
- k = j ;
- temp = stu[k] ;
- stu[k] = stu[i] ;
- stu[i] = temp ;
- }
- }
- }
- for(i = 0 ; i < 10 ; i ++) printf("%6d %8s %6.2f\n",stu[i].num , stu[i] . name , stu[i] . score) ;
- }
复制代码
编译、运行实况:
- D:\00.Excise\C>g++ -o x x.c
- D:\00.Excise\C>x
- 1 zyz 1
- 2 gl 2
- 3 zsc 3
- 4 abc 4
- 5 bcd 5
- 6 cde 6
- 7 def 7
- 8 efg 8
- 9 fgh 9
- 10 ghi 10
- 10 ghi 10.00
- 9 fgh 9.00
- 8 efg 8.00
- 7 def 7.00
- 6 cde 6.00
- 5 bcd 5.00
- 4 abc 4.00
- 3 zsc 3.00
- 2 gl 2.00
- 1 zyz 1.00
- D:\00.Excise\C>
复制代码
|
|