题解 | 成绩排序

成绩排序

https://www.nowcoder.com/practice/0383714a1bb749499050d2e0610418b1

#include <stdio.h>
typedef struct student{
    char s[100];
    int g;
} students[1000];
 students s;
 int a, b;
 void switch_g(int x,int y){
    struct student temp=s[x];
    s[x]=s[y];
    s[y]=temp;
 }
//冒泡排序
void solve0(){
  
    for(int i=1;i<a;i++){
        for(int j=1;j<=a-i;j++){
            if(s[j].g<s[j+1].g){
                switch_g(j, j+1);
            }
        }
    }
}
void solve1(){
  
    for(int i=1;i<a;i++){
        for(int j=1;j<=a-i;j++){
            if(s[j].g>s[j+1].g){
                switch_g(j, j+1);
            }
        }
    }
}
int main() {
    
   
    while (scanf("%d %d", &a, &b) != EOF) { // 注意 while 处理多个 case
        // 64 位输出请用 printf("%lld") to 
        for(int i=1;i<=a;i++){
            scanf("%s %d",s[i].s,&s[i].g);
        }
        if(b==1){
            solve1();
        }
        else{
            solve0();
        }
        for(int i=1;i<=a;i++){
            printf("%s %d\n",s[i].s,s[i].g);
        }
    }
    return 0;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务