注册 登录
编程论坛 C++教室

出场顺序C++求解大神们新手上路

ljp294777643 发布于 2020-08-20 13:15, 2733 次点击
3. 出场顺序
【问题描述】
元宵节晚会上,参加节目表演的同学需要抽签决定出场顺序,序号小的同学先出场, 现在已知n(0<n<100)位同学的姓名和抽到的号码(无重复),请将同学们的姓名,
按照出场次序输出。
输入:第一行为一个数字n,表示班上有n名同学的信息。
 接下来有n行,每行包括同学的姓名和出场号,以空格隔开。
输出:n行,每行表示一个按照出场号排好的同学姓名。 注:假设班上无同名的学生。
【样例输入】
5
xinyu 4
haomiao 3
tiansen 6
jintao 2
zhangze 5
【样例输出】
jintao
haomiao
xinyu
zhangze
tiansen
2 回复
#2
雪影辰风2020-08-20 14:24
程序代码:
#include <algorithm>
#include <cstdio>
using namespace std;
struct Sim
{
    char name[255];
    int num;
};
inline bool Cmp(Sim a, Sim b)
{
    return a.num < b.num;
}
int main()
{
    int n;
    scanf("%d", &n);
    Sim a[n + 1];
    for (int i = 1; i <= n; i++)
        scanf("%s%d", a[i].name, &a[i].num);
    sort(a + 1, a + 1 + n, Cmp);
    for (int i = 1; i <= n; i++)
        printf("%s\n", a[i].name, a[i].num);
    return 0;
}
#3
rjsp2020-08-20 15:17
程序代码:
#include <iostream>
#include <iterator>
#include <algorithm>
#include <tuple>
#include <string>
#include <span>
using namespace std;

int main( void )
{
    size_t n;
    cin >> n;

    tuple<string,int> buf[100];
    span arr(buf,n);
    for( auto& v : arr )
        cin >> get<0>(v) >> get<1>(v);

    sort( begin(arr), end(arr), [](const auto& a,const auto& b){return get<1>(a) < get<1>(b);} );

    for( const auto& v : arr )
        cout << get<0>(v) << '\n';
}
1