【C++】AOJ基础题 ITP2_5_B Sorting Tuples

Sorting Tuples

Write a program which reads n items and sorts them. Each item has attributes {value, weight, type, date, name} and they are represented by { integer, integer, upper-case letter, integer, string } respectively. Sort the items based on the following priorities.

  1. first by value (ascending)
  2. in case of a tie, by weight (ascending)
  3. in case of a tie, by type (ascending in lexicographic order)
  4. in case of a tie, by date (ascending)
  5. in case of a tie, by name (ascending in lexicographic order)

Input

The input is given in the following format.

n
v0 w0 t0 d0 s0
v1 w1 t1 d1 s1
:
vn−1 wn−1 tn−1 dn−1 sn−1

In the first line, the number of items n. In the following n lines, attributes of each item are given. vi wi ti di si represent value, weight, type, date and name of the i-th item respectively.

Constraints

1

Output

Print attributes of each item in order. Print an item in a line and adjacency attributes should be separated by a single space.

Sample Input

5
105 24 C 1500000000000 white
100 23 C 1500000000000 blue
105 23 A 1480000000000 pink
110 25 B 1500000000000 black
110 20 A 1300000000000 gree

Sample Output

100 23 C 1500000000000 blue
105 23 A 1480000000000 pink
105 24 C 1500000000000 white
110 20 A 1300000000000 gree
110 25 B 1500000000000 black

問題を解く

#include <stdio.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

typedef long long ll;

struct item {
    int v;
    int w;
    char t;
    ll d;
    string s;
};

bool compare(const item& a, const item& b) {
    if (a.v == b.v) 
    {
        if (a.w == b.w) 
        {
            if (a.t == b.t) 
            {
                if (a.d == b.d) 
                {
                    return a.s < b.s;
                }
                return a.d < b.d;
            }
            return a.t < b.t;
        }
        return a.w < b.w;
    }
    return a.v < b.v;
}

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

    vector<item> p(n);

    for (int i = 0; i < n; i++) 
    {
        cin >> p[i].v >> p[i].w >> p[i].t >> p[i].d >> p[i].s;
    }

    sort(p.begin(), p.end(), compare);

    for (int i = 0; i < n; i++) 
    {
        cout << p[i].v << " " << p[i].w << " " << p[i].t << " " << p[i].d << " " << p[i].s << endl;
    }

    return 0;
}

总结

  • 用结构体构造vector是很棒的思路,mark一下sort的用法,第三个参数即排序标准。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值