vs19c++成绩排名

用到了strcmp、swap函数

一、介绍

输入:

第一行包含一个正整数N,表示有个人参加了考试。接下来N行,每行有一个字符串和一个正整数,分别表示人名和对应的成绩,用一个空格分隔。

人数<=100,分数<=100,人名仅包含小写字母。

输出:

输出一共有N行,每行一个字符串,第i行的字符串表示成绩从高到低排在第i位的人的名字,若分数一样则按人名的字典序顺序从小到大。

二、代码部分

typedef struct与struct定义结构体的区别见【知识笔记】

#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
typedef struct {
    char s[100];
    int score;
}student;
int n;

int main()
{
    student s[100];
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> s[i].s >> s[i].score;
    }
    for (int j = 1; j < n; j++) {
        for (int i = 0; i < n - 1; i++) {
            if (s[i].score < s[i + 1].score) {
                swap(s[i], s[i + 1]);
            }
            else if (s[i].score == s[i + 1].score) {
                int a = strcmp(s[i].s, s[i + 1].s);
                if (a > 0)
                    swap(s[i], s[i + 1]);
            }
        }
    }
    for (int i = 0; i < n; i++) {
        cout << s[i].s << endl;
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值