题解 | #成绩排序#

成绩排序

https://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b

#include <stdio.h>
struct score {
    char name[10];
    int s;
};
int main() {
    int n = 0;
    int x = 0;
    scanf("%d", &n);
    scanf("%d", &x);
    struct score str[200];
    for (int i = 0; i < n; i++)
        scanf("%s%d", str[i].name, &str[i].s);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n - 1 - i; j++) {
            if (x == 1) {
                if (str[j].s > str[j + 1].s) {
                    struct score tmp = str[j];
                    str[j] = str[j + 1];
                    str[j + 1] = tmp;
                }
            } else if (x == 0)
            {
                if (str[j].s < str[j + 1].s) {
                    struct score tmp = str[j];
                    str[j] = str[j + 1];
                    str[j + 1] = tmp;
                }
            }
    }
}
    for (int i = 0; i < n; i++)
        printf("%s %d\n", str[i].name, str[i].s);

return 0;
}

全部评论

相关推荐

冷艳的小师弟在看机会:jd测评乱点直接被挂了,哭死~
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务