这道题我用的方法很麻烦,使用一个结构体vector保存结果,使用一个map判断是不是一个人报了两个工程.使用set判断一个工程是不是有人重复报名
于是就有了下面的笨拙代码
1#include <string>
2#include <set>
3#include <vector>
4#include <memory.h>
5#include <map>
6#include <algorithm>
7using namespace std;
8
9struct Project
10{
11 string name;
12 int studentcount;
13 Project()
14 {
15 studentcount=0;
16 name.assign("");
17 }
18};
19
20map<string,int> studentmap;
21set<string> studentset;
22vector<Project> prov;
23int countarray[101]={0};
24char strtemp[100]="";
25Project project;
26
27inline bool comp(const Project &p1,const Project &p2)
28{
29 return p1.studentcount>p2.studentcount;
30}
31inline bool comp2(const Project &p1,const Project &p2)
32{
33 return p1.name.compare(p2.name)<0;
34}
35
36int main()
37{
38
39 while(true)
40 {
41 int projcount=0;
42 prov.push_back(project);
43 while(true)
44 {
45 gets(strtemp);
46 project.name.assign(strtemp);
47 if(project.name[0]=='1')break;
48 if(project.name[0]=='0')exit(0);
49 if(project.name[0]>='A'&&project.name[0]<='Z')
50 {
51 prov.push_back(project);
52 studentset.clear();
53 projcount++;
54 }
55 if(project.name[0]>='a'&&project.name[0]<='z')
56 {
57 if(studentmap[strtemp]==0)
58 {
59 studentmap[strtemp]=projcount;
60 }
61 else if(studentmap[strtemp]==-1)
62 {
63 continue;
64 }
65 else if(studentmap[strtemp]!=projcount)
66 {
67 prov[studentmap[strtemp]].studentcount--;
68 studentmap[strtemp]=-1;
69 continue;
70 }
71 if(studentset.count(strtemp))
72 continue;
73 else
74 {
75 studentset.insert(strtemp);
76 prov[projcount].studentcount++;
77 }
78 }
79 }
80 prov.erase(prov.begin());
81 stable_sort(prov.begin(),prov.end(),comp2);
82 stable_sort(prov.begin(),prov.end(),comp);
83 for(int i=0;i<prov.size();i++)
84 {
85 printf("%s %d\n",prov[i].name.c_str(),prov[i].studentcount);
86 }
87 memset(countarray,0,sizeof(countarray));
88 studentmap.clear();
89 prov.clear();
90
91 }
92}
93
94