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

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

img

马上

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;
}
#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

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



#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;
    }
#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;
}