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. |
Input |
This question is only one set of test data |
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). . . . .