Title Description
A few days ago, we have just experienced a typhoon hit Zhejiang Province, the relevant department for statistics about the losses in various places, according to the extent of losses in descending order.
Entry
An integer \ (N \ Leq 10 \) .
Here \ (N \) lines, each line are the name of the place, and the amount of loss of this typhoon.
Export
According to the amount of loss, sorted from high to low
Sample input
5
linhai 100
shaoxing 60
hangzhou 80
jiaxing 30
tiantai 70
Sample Output
linhai 100
hangzhou 80
tiantai 70
shaoxing 60
jiaxing 30
Ideas & Answers
Classic sort of water problem.
First, we need to build a structure struct node
, which contains information about each of the refugees.
string name
: Name of each refugee.
int money
: Amount per refugee loss.
Implementation:
struct node
{
string name;
int money;
};
In the custom sort function cmp(node a,node b)
:
bool cmp(node a,node b)
{
return a.money > b.money; //从损失程度高到损失程度低
}
The last call sort()
function can be.
Code
#include <bits/stdc++.h>
using namespace std;
struct node
{
string name;
int money;
};
bool cmp(node a, node b)
{
return a.money > b.money;
}
int main()
{
node p[12];
int n;
cin >> n;
for (int i = 1; i <= n; ++i)
cin >> p[i].name >> p[i].money;
sort(p + 1, p + n + 1, cmp);
for (int i = 1; i <= n; ++i)
cout << p[i].name << ' ' << p[i].money << endl;
return 0;
}
/**************************************************************
Reach-Top OJ
RID:350347
code length:398 B
Problem: 2909
User: 2018summerXC82
Language: C++
Result: 正确
Time:0 ms
Memory:1720 kb
****************************************************************/