Sorting binding structure and sort (STL) of

Topics are as follows:

 

Description

Ever since e-sports in China has been identified for the first 99 official sports, the leader will join them, CS (Counter Strike Counter Strike) is good at his game, there are a lot of pros to pay.
Counter-Strike has a variety of firearms, we To simplify matters, the provisions of each gun has its own name, lethality, price.
Here you follow the requirements of the following provisions to known firearms Sort:
Requirements sorted (descending) attack guns, gun attack if some of these guns is the same as sorting by price (from small to large), if some of the same gun prices in accordance with the lexicographic order of the names of these guns. (In this problem, if the string a, b satisfy strcmp (a, b) <0 is met lexicographic order)

Input

This question is only one set of test data
of the first line of: N (1 <= N < = 100000) expressed gun N kinds
of 1..1 + N line: First, the name of a firearm (including only uppercase letters, numbers, accounting for up to 10 characters), followed by two numbers A (0 <= A <= 1000) B (0 <= B <= 1000) A representative of lethality, B represents the price

Output

After sorting, each line of output the name of one kind of gun

Sample Input
7
AK47 500,200
AWP 500 1000
USP 50 20
M4A1 500 300
MP5 200 100
MP3 200 100
MP4 200 100
Sample Output
The AWP
AK47
the M4A1
the MP3
MP4
MP5
the USP

Ideas:

C ++ can define the structure, and we just need to sort of structure on it.

We all know that in C ++ STL has a sort function:

sort (), its template is:

 

sort (start, end, sorting method);

 

And we just need to override the ordering method is OK,

Therefore, we need to define a bool-type function (int type may also be in C / C ++ in 0 flase, 1 is true).
The collation, in accordance with a descending element, an element according to the small to large, according to another lexicographic order, so
cmp function can be defined as follows:

 

bool cmp (const gun &s1,const gun &s2)

{

    if(s1.b!=s2.b)

        return s1.b>s2.b;

    if(s1.m!=s2.m)

        return s1.m<s2.m;

    char *w=(char *)s1.a;

    char *e=(char *)s2.a;

    if (strcmp(w,e)<0)

        return 1;

    else

        return 0;

}

 

Attach AC Code :

 

#include <iostream>

#include <string.h>

#include <math.h>

#include <algorithm>

#include <stdio.h>

using namespace std;

 

struct gun {

    char a[15];

    int b;

    int m;

};

bool cmp (const gun &s1,const gun &s2)

{

    if(s1.b!=s2.b)

        return s1.b>s2.b;

    if(s1.m!=s2.m)

        return s1.m<s2.m;

    char *w=(char *)s1.a;

    char *e=(char *)s2.a;

    if (strcmp(w,e)<0)

        return 1;

    else

        return 0;

}

 

int main ()

{

    int n;

    while(~scanf("%d",&n))

    {

        gun q[n];

        for(int i=0;i<n;i++)

        {

            //cin>>q[i].a>>q[i].b>>q[i].m;

            scanf("%s",q[i].a);

            scanf("%d%d",&q[i].b,&q[i].m);

        }

        sort(q,q+n,cmp);

        for(int i=0;i<n;i++)

        {

            //cout<<q[i].a<<endl;

            //puts(q[i].a);

            printf("%s\n",q[i].a);

        }

    }

    return 0;

}

This problem is there is a large pit ..... test data, if you use cin, cout time out. . . . (Do not ask how I know). . . . .

 

Guess you like

Origin blog.csdn.net/hrbust_cxl/article/details/79666087