vector<int> numberExam(list<int> &data, int M){ vector<int> result; auto it = data.begin(); while (data.size() >= M) { for (int i = 1; i < M; ++i) { ++it; if (it == data.end()) { it = data.begin(); // 确保循环 } } it = data.erase(it); if (it == data.end()) { it = data.begin(); } }
for (constauto &kItem : data) { result.push_back(kItem); }
return result; }
intmain(){
int M; cin >> M;
list<int> data; for (int i = 1; i <= people_num; ++i) { data.push_back(i); }
if (M == 1 || M >= 100) { cout << "ERROR!" << endl; return0; }
auto re = numberExam(data, M);
for (int i = 0; i < re.size(); ++i) { if (i == 0) { cout << re[i]; } else { cout << "," << re[i]; } }
return0; }
最关键的就是 numberExam 函数,我觉得时刻要关注的就是 it
迭代器是不是到达 end(),时刻检查并且把 it 更新为 begin() 迭代器。