pku3297解題報(bào)告
這道題我用的方法很麻煩,使用一個(gè)結(jié)構(gòu)體vector保存結(jié)果,使用一個(gè)map判斷是不是一個(gè)人報(bào)了兩個(gè)工程.使用set判斷一個(gè)工程是不是有人重復(fù)報(bào)名
于是就有了下面的笨拙代碼
1
#include <string>
2
#include <set>
3
#include <vector>
4
#include <memory.h>
5
#include <map>
6
#include <algorithm>
7
using namespace std;
8
9
struct Project
10

{
11
string name;
12
int studentcount;
13
Project()
14
{
15
studentcount=0;
16
name.assign("");
17
}
18
};
19
20
map<string,int> studentmap;
21
set<string> studentset;
22
vector<Project> prov;
23
int countarray[101]=
{0};
24
char strtemp[100]="";
25
Project project;
26
27
inline bool comp(const Project &p1,const Project &p2)
28

{
29
return p1.studentcount>p2.studentcount;
30
}
31
inline bool comp2(const Project &p1,const Project &p2)
32

{
33
return p1.name.compare(p2.name)<0;
34
}
35
36
int 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

2

3

4

5

6

7

8

9

10



11

12

13

14



15

16

17

18

19

20

21

22

23



24

25

26

27

28



29

30

31

32



33

34

35

36

37



38

39

40



41

42

43

44



45

46

47

48

49

50



51

52

53

54

55

56



57

58



59

60

61

62



63

64

65

66



67

68

69

70

71

72

73

74



75

76

77

78

79

80

81

82

83

84



85

86

87

88

89

90

91

92

93

94

posted on 2007-07-28 16:37 Gohan 閱讀(378) 評論(0) 編輯 收藏 引用 所屬分類: C++ 、Practise