HDU 3293 sort
结构体排序,sort函数的运用!大意是名字不同的情况下,名字先按字典顺序排列,同名者看武器的等级,wonderful的先,good第二,soso最后!
武器等级一样的同样按字典顺序排列!
1 #include <iostream>
2 #include <stdio.h>
3 #include <string.h>
4 #include <algorithm>
5 usingnamespace std;
6 struct node
7 {
8 char weapon1[21];
9 char name[21];
10 char rank[21];
11 int level;
12 };
13 node nodes[510];
14 int cmp(node a,node b)
15 {
16 if(strcmp(b.name,a.name)==0)
17 {
18 if(strcmp(b.rank,a.rank)!=0)
19 return a.level<b.level;
20 else
21 if(strcmp(b.weapon1,a.weapon1)>0)
22 return1;
23 else
24 return0;
25 }
26 else
27 if(strcmp(b.name,a.name)>0)
28 return1;
29 else
30 return0;
31
32
33 }
34 int main()
35 {
36 int t=1;
37 int n,i;
38 while(scanf("%d",&n)!=EOF)
39 {
40 for(i=0;i<n;i++)
41 {
42 cin>>nodes[i].weapon1>>nodes[i].name>>nodes[i].rank;
43 if(strcmp(nodes[i].rank,"wonderful")==0)
44 nodes[i].level=1;
45 else
46 if(strcmp(nodes[i].rank,"good")==0)
47 nodes[i].level=2;
48 else nodes[i].level=3;
49
50 }
51 sort(nodes,nodes+n,cmp);
52 cout<<"Case "<<t++<<endl;
53 for(i=0;i<n;i++)
54 {
55 if(strcmp(nodes[i-1].name,nodes[i].name)!=0)
56 cout<<nodes[i].name<<":"<<endl;
57 cout<<""<<nodes[i].weapon1<<""<<nodes[i].rank<<endl;
58 }
59 }
60 return0;
61
62 }
应该不用写注释了吧!我感觉都还好!有不懂得可以留言我会解答的!