鱼C论坛

 找回密码
 立即注册
查看: 1162|回复: 2

[已解决]如何输入学生信息后按成绩排序

[复制链接]
发表于 2022-1-1 11:41:27 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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;
}
求问这个代码怎么行不通
最佳答案
2022-1-1 12:08:31
  1. #include <stdio.h>

  2. struct Student
  3. {
  4.         int num       ;
  5.         char name[20] ;
  6.         float score   ;
  7. } stu[10]             ;

  8. int main(void)
  9. {
  10.         int i , j , k ; struct Student temp                                                            ;
  11.         for(i = 0 ; i < 10 ; i ++) scanf("%d%s%f" , & stu[i] . num , stu[i] . name , & stu[i] . score) ;
  12.         for(i = 0 ; i < 9 ; i ++) {
  13.                 for(k = i , j = i + 1 ; j < 10 ; j ++) {
  14.                         if(stu[j] . score > stu[k] . score) {
  15.                                 k = j           ;
  16.                                 temp = stu[k]   ;
  17.                                 stu[k] = stu[i] ;
  18.                                 stu[i] = temp   ;
  19.                         }
  20.                 }
  21.         }
  22.         for(i = 0 ; i < 10 ; i ++) printf("%6d %8s %6.2f\n",stu[i].num , stu[i] . name , stu[i] . score) ;
  23. }
复制代码

        编译、运行实况:
  1. D:\00.Excise\C>g++ -o x x.c

  2. D:\00.Excise\C>x
  3. 1 zyz 1
  4. 2 gl 2
  5. 3 zsc 3
  6. 4 abc 4
  7. 5 bcd 5
  8. 6 cde 6
  9. 7 def 7
  10. 8 efg 8
  11. 9 fgh 9
  12. 10 ghi 10
  13.     10      ghi  10.00
  14.      9      fgh   9.00
  15.      8      efg   8.00
  16.      7      def   7.00
  17.      6      cde   6.00
  18.      5      bcd   5.00
  19.      4      abc   4.00
  20.      3      zsc   3.00
  21.      2       gl   2.00
  22.      1      zyz   1.00

  23. D:\00.Excise\C>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-1-1 12:08:31 | 显示全部楼层    本楼为最佳答案   
  1. #include <stdio.h>

  2. struct Student
  3. {
  4.         int num       ;
  5.         char name[20] ;
  6.         float score   ;
  7. } stu[10]             ;

  8. int main(void)
  9. {
  10.         int i , j , k ; struct Student temp                                                            ;
  11.         for(i = 0 ; i < 10 ; i ++) scanf("%d%s%f" , & stu[i] . num , stu[i] . name , & stu[i] . score) ;
  12.         for(i = 0 ; i < 9 ; i ++) {
  13.                 for(k = i , j = i + 1 ; j < 10 ; j ++) {
  14.                         if(stu[j] . score > stu[k] . score) {
  15.                                 k = j           ;
  16.                                 temp = stu[k]   ;
  17.                                 stu[k] = stu[i] ;
  18.                                 stu[i] = temp   ;
  19.                         }
  20.                 }
  21.         }
  22.         for(i = 0 ; i < 10 ; i ++) printf("%6d %8s %6.2f\n",stu[i].num , stu[i] . name , stu[i] . score) ;
  23. }
复制代码

        编译、运行实况:
  1. D:\00.Excise\C>g++ -o x x.c

  2. D:\00.Excise\C>x
  3. 1 zyz 1
  4. 2 gl 2
  5. 3 zsc 3
  6. 4 abc 4
  7. 5 bcd 5
  8. 6 cde 6
  9. 7 def 7
  10. 8 efg 8
  11. 9 fgh 9
  12. 10 ghi 10
  13.     10      ghi  10.00
  14.      9      fgh   9.00
  15.      8      efg   8.00
  16.      7      def   7.00
  17.      6      cde   6.00
  18.      5      bcd   5.00
  19.      4      abc   4.00
  20.      3      zsc   3.00
  21.      2       gl   2.00
  22.      1      zyz   1.00

  23. D:\00.Excise\C>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-1-1 12:09:42 | 显示全部楼层
本帖最后由 傻眼貓咪 于 2022-1-1 12:11 编辑
  1. {scanf("%d,%s,%f",&stu[i].num, stu[i].name,&stu[i].score);}
复制代码
[b] // 输入字符串时,试试去掉 &
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-25 02:42

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表