九度OJ1005题 一直WA??
题目链接:http://ac.jobdu.com/problem.php?pid=1005
感觉自己下面写的程序没有问题,但一直就是WA。程序也不求效率,只求能通过。怎么就一直有问题,真是看不出哪里有边界问题没考虑了?哪位朋友帮忙看下哪里有问题。
1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 5 using namespace std; 6 7 8 class Stu 9 { 10 public: 11 12 Stu(int ge, int gi, int id) : GE(ge), GI(gi), StuId(id) {} 13 14 // 这几个函数主要是因为private的问题,所以写成 成员函数的样子来调用 15 void setChoice(int _choice) { choice.push_back(_choice); } 16 int getChoice(int pos) const { return choice[pos]; } 17 int getStuId() const { return StuId; } 18 19 //后面sort时候用,根据题目要求来排序 20 bool operator<(const Stu& rhs) const 21 { 22 if ( (GE+GI) != (rhs.GE+rhs.GI) ) 23 { 24 return (GE+GI) > (rhs.GE+rhs.GI); 25 } 26 else 27 { 28 return GE > rhs.GE; 29 } 30 } 31 32 // 判断两个学生的成绩是否相等,用于处理学校名额已经没了,但这个同学可以录取的情况 33 bool operator==(const Stu& rhs) const 34 { 35 return ( !(*this < rhs) ) && ( !(rhs < *this) ); 36 } 37 38 private: 39 int GE; 40 int GI; 41 int StuId; 42 vector<int> choice; 43 44 }; 45 46 class School 47 { 48 public: 49 School(int _quota) : quota(_quota), freeQuota(_quota) {} 50 void subQuota() { --freeQuota; } //每加入一个学生后,配额就-1,(除了最后已经没配额,还加入学生的情况) 51 int getFreeQuota() { return freeQuota; } //返回还剩多少配额 52 int getQuota() { return quota; } //返回输入时定义的配额,即程序里面的M 53 void pushStuId(int stuid) { StuIdVec.push_back(stuid); } 54 int getLastStuId() { return StuIdVec.back();} //返回这个学校里面最后一个学生的id,用来在后面判断其他学生是否也能进来这个学校 55 void printStuId() //所有学生处理完后,打印结果 56 { 57 if (StuIdVec.size() != 0) 58 { 59 sort(StuIdVec.begin(), StuIdVec.end()); 60 size_t idx = 0; 61 for (; idx != StuIdVec.size() -1; ++idx) { cout << StuIdVec[idx] << ' '; } 62 cout << StuIdVec[idx] << endl; 63 } 64 else 65 { 66 cout << endl; 67 } 68 } 69 70 private: 71 int quota; 72 int freeQuota; 73 vector<int> StuIdVec; 74 }; 75 76 int main() 77 { 78 int N = 0; 79 int M = 0; 80 int K = 0; 81 82 while ( cin >> N >> M >> K ) 83 { 84 vector<School> Mvec; 85 vector<Stu> Nvec; 86 87 // 输入学校配额信息 88 for (int idxM = 0, tmp = 0; idxM != M; ++idxM) 89 { 90 cin >> tmp; 91 School m_school(tmp); 92 Mvec.push_back(m_school); 93 } 94 95 // 输入学生信息 96 for (int idxN = 0; idxN != N; ++idxN) 97 { 98 int ge = 0; 99 int gi = 0; 100 cin >> ge >> gi; 101 Stu m_stu(ge, gi, idxN); 102 for (int idxK = 0, tmp = 0; idxK != K; ++idxK) 103 { 104 cin >> tmp; 105 m_stu.setChoice(tmp); 106 } 107 Nvec.push_back(m_stu); 108 } 109 110 // 按照定义的排序准则对学生成绩进行排序 111 sort(Nvec.begin(), Nvec.end()); 112 113 // 处理学生入校的问题 114 for (size_t idxN = 0; idxN != Nvec.size(); ++idxN) //这么多个学生信息 115 { 116 for (int idxK = 0; idxK != K; ++idxK) //判断这个学生的每个选择是否可行 117 { 118 int choiceK = Nvec[idxN].getChoice(idxK); //得到他的第idxK个选择 119 120 if (Mvec[choiceK].getFreeQuota() != 0) //第idxK个选择的学校还有配额,直接加入这个学生 121 { 122 Mvec[choiceK].pushStuId(Nvec[idxN].getStuId()); 123 Mvec[choiceK].subQuota(); 124 break; 125 } 126 else if (Mvec[choiceK].getQuota() != 0) //配额为0情况,需要判断是这个学校就没有配额,还是配额已经被其他学生用完了。 127 { 128 if ( Nvec[Mvec[choiceK].getLastStuId()] == Nvec[idxN]) //已经没其他学生用完了,但这个学生也可以入校 129 { 130 Mvec[choiceK].pushStuId(Nvec[idxN].getStuId()); 131 break; 132 } 133 } 134 } 135 } 136 137 // 输出所有学校最好的录取信息 138 for (int idx = 0; idx != M; ++idx) 139 { 140 Mvec[idx].printStuId(); 141 } 142 143 } 144 145 return 0; 146 }

浙公网安备 33010602011771号