C/C++程式設計題之圓桌遊戲
阿新 • • 發佈:2019-02-14
n個人圍坐在一個圓桌上從1到n編號,順時針從第一個人開始報數,從1開始報到m(m < n),
報到m的那個人出圓桌,再從出桌的人的下一個人從1開始報數,直到圓桌上的人不足m人位置,
輸出最終剩下人的編號。
bool RoundTable(int n,int m,list<int>& personList) { personList.swap(list<int>()); if (n <= m) { return false; } for (int i = 1; i <= n; i++) { personList.push_back(i); } list<int>::iterator itePerson = personList.begin(); int count = 1; while(personList.size() >= m) { if (count == m) { itePerson = personList.erase(itePerson); count = 1; continue; } if (itePerson == personList.end()) { itePerson = personList.begin(); continue; } itePerson++; count++; } return true; }
list容器中剩餘的編號就是圓桌中還剩餘的人。