小屿睡醒了没 2022-11-28 22:24 采纳率: 60%
浏览 46
已结题

C语言简单排序问题有偿求解

在主函数里已给定成绩,学号,姓名,三者从左至右一一对应;写一个排序函数,按成绩从高到低排序,来实现信息记录的调整,保证学号,姓名和成绩对应得上

img

  • 写回答

5条回答 默认 最新

  • 语言-逆行者 2022-11-28 22:28
    关注

    马上

    img

    #include<stdio.h>
    int main(){
        int score[10]={65,88,90,98,78,86,77,90,95,18};
        int num[10]={1001,1002,1003,1004,1005,1006,1007,1008,1009,1010};
        char Name[10][30]={"Zhangsan","Zhaopeng","Lili","Lule","Xiaoxiao","Zhanle","Keke","Liulu","Changchang","Sunhe"};
        int scoreindex[10];
        for(int i=0;i<10;i++){
            score[i]=score[i]*100+i;
        }
        //冒泡排序
        for (int i = 0; i < 9; i++)//i<9进行解释①
        {
            for (int j = 0; j < 9-i; j++)//i<9-i进行解释②
            {
                if (score[j] > score[j + 1])//满足条件进行交换
                {
                    int temp = score[j];
                    score[j] = score[j + 1];
                    score[j + 1] = temp;
                }
            }
        }
        printf("学号         姓名       成绩:\n");
        for(int i=9;i>=0;i--){
            int index;
            index=score[i]%100;
            printf("%d%15s     %d\n",num[index],Name[index],score[i]/100);
        }
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
  • 叶落花枯 2022-11-28 22:56
    关注
    #include <stdio.h>
    #include <string.h>
    
    // 冒泡排序
    void sorr(int score[], int num[], char name[][30], int n)
    {
        int i, j;
        int s, t;
        char b[30];
        for(i=0; i<n; i++) {
            for(j=0; j<n-1-i; j++) {
                if(score[j] < score[j + 1]) {
                    s = score[j];
                    score[j] = score[j + 1];
                    score[j + 1] = s;
    
                    t = num[j];
                    num[j] = num[j + 1];
                    num[j + 1] = t;
    
                    strcpy(b, name[j]);
                    strcpy(name[j], name[j + 1]);
                    strcpy(name[j + 1], b);
                }
            }
        }
        printf("Num\tName\tScore\n");
        for(i=0; i<n; i++) {
            printf("%d\t%d\t%s\n", num[i], score[i], name[i]);
        }
    }
    
    
    int main()
    {
        int score[10] = {65,88,90,98,78,86,77,90,95,18};
        int num[10] = {1001,1002,1003,1004,1005,1006,1007,1008,1009,1010};
        char Name[10][30] = {"Zhangsan","Zhaopeng","Lili","Lule","Xiaoxiao","Zhaole","Keke","Liulu","Changchang","Sunhe"};
        sorr(score, num, Name, 10);
    
        return 0;
    }
    

    img

    评论
  • cyjbj 2022-11-29 09:04
    关注

    排序算法多了去了,唯一需要注意的就是在成绩排序的同时把成绩对应的学号和姓名一块带着就ok了

    评论
  • yy64ll826 2022-11-29 17:34
    关注
    
    
    #include<stdio.h>
    #include<stdlib.h>
        struct Student {
            int xuehao;
            char name[20];
            float score;
        };1
        int paixu(const void* a, const void* b)
        {
            struct Student* aa = (struct Student*)a;1
            struct Student* bb = (struct Student*)b;1
            return (aa->score < bb->score ? 1 : -1);2
        }
        int main()
        {
            struct Student chucun[5] = { {10101,"Zhang",78},{10103,"Wang",98.5},{10106,"Li",86},{10108,"Ling",73.5},{10110,"Fun",100}};
            qsort(chucun, 5, sizeof(chucun[0]), paixu);
            int i;
            for (i = 0; i < 5; i++)
            {
                printf("%d %s %.2f\n", chucun[i].xuehao, chucun[i].name,chucun[i].score);
            }
            return 0;
        }
    
    评论
  • fuill 2022-11-29 02:52
    关注
    #include <stdio.h>
    #include <string.h>
    // 冒泡排序
    void sorr(int score[], int num[], char name[][30], int n)
    {
        int i, j;
        int s, t;
        char b[30];
        for(i=0; i<n; i++) {
            for(j=0; j<n-1-i; j++) {
                if(score[j] < score[j + 1]) {
                    s = score[j];
                    score[j] = score[j + 1];
                    score[j + 1] = s;
                    t = num[j];
                    num[j] = num[j + 1];
                    num[j + 1] = t;
                    strcpy(b, name[j]);
                    strcpy(name[j], name[j + 1]);
                    strcpy(name[j + 1], b);
                }
            }
        }
        printf("Num\tName\tScore\n");
        for(i=0; i<n; i++) {
            printf("%d\t%d\t%s\n", num[i], score[i], name[i]);
        }
    }
    int main()
    {
        int score[10] = {65,88,90,98,78,86,77,90,95,18};
        int num[10] = {1001,1002,1003,1004,1005,1006,1007,1008,1009,1010};
        char Name[10][30] = {"Zhangsan","Zhaopeng","Lili","Lule","Xiaoxiao","Zhaole","Keke","Liulu","Changchang","Sunhe"};
        sorr(score, num, Name, 10);
        return 0;
    }
    
    评论
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 12月7日
  • 已采纳回答 11月29日
  • 请采纳用户回复 11月29日
  • 创建了问题 11月28日

悬赏问题

  • ¥30 android百度地图SDK海量点显示标题
  • ¥15 windows导入environment.yml运行conda env create -f environment_win.yml命令报错
  • ¥15 这段代码可以正常运行,打包后无法执行,在执行for内容之前一直不断弹窗,请修改调整
  • ¥15 C语言判断有向图是否存在环路
  • ¥15 请问4.11到4.18以及4.27和4.29公式的具体推导过程是怎样的呢
  • ¥20 将resnet50中的卷积替换微ODConv动态卷积
  • ¥15 通过文本框输入商品信息点击按钮将商品信息列举出来点击加入购物车商品信息添加到表单中
  • ¥100 这是什么压缩算法?如何解压?
  • ¥20 upload上传实验报错500,如何解决?(操作系统-windows)
  • ¥15 谁知道 ShaderGraph 那个节点可以接入 Particle System -> Custom Data