文章

145

粉丝

144

获赞

21

访问

47.3k

头像
成绩排序 题解:
P1151 清华大学上机题
发布于2024年1月27日 15:37
阅读数 478

#include <stdio.h>
#define N 1000
#define M 10
typedef struct{
    char Sno[N];
    int s; 
}Student;

//按排序(冒泡排序:稳定)
void Sort(Student stu[],int n,int b)
{
    Student temp;
    int i,j;
    for(i = 0; i < n-1; i++)
        for(j = 1; j < n-i; j++)
            if(b == 1)//升序
            {
                if(stu[j-1].s > stu[j].s)
                {
                    temp = stu[j];
                    stu[j] = stu[j-1];
                    stu[j-1] = temp;
                }
            }
            else {//降序
                if(stu[j-1].s < stu[j].s)
                {
                    temp = stu[j];
                    stu[j] = stu[j-1];
                    stu[j-1] = temp;
                }
            }
}

int main() {
    Student stu[N]; 
    int a, b, i,j = 0;
    while (scanf("%d %d", &a, &b) != EOF) {
        for(i = 0; i < a; i++)
            scanf("%s %d",stu[i].Sno,&stu[i].s);
        Sort(stu,a...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发