编号为1,2,3,......,n的n个人按顺时针方向转一圈。任选一个正整数作为报数上限m,从第一个人开始顺序报数,报到m时停止报数。报m的人出列,从他在顺时针方向上的下一个人开始重新从1报数,如此下去,直至所有人都出列为止。设计程序输出出列顺序。
#include <iostream>
#include <cstring>
using namespace std;
struct People {
string name;
long long int num;
string gender;
int age;
string Banji;
string health;
int data;
People* next;
};
class Joseph {
public:
Joseph(int n);
void Print(int m);
private:
int num;
People* first;
};
int main() {
int n, m;
cin >> n >> m;
Joseph J(n);
J.Print(m);
}
Joseph::Joseph(int n)
{
num = n;
People* p = new People;
first = p;
first->next = first;
p->data = 1;
cin >> p->name >> p->num >> p->gender >> p->age;
cin >> p->Banji >> p->health;
for (int i = 2; i <= num; i++) {
p = new People;
p->data = i;
cin >> p->name >> p->num >> p->gender >> p->age;
cin >> p->Banji >> p->health;
p->next = first->next;
first->next = p;
first = p;
}
}
void Joseph::Print(int m)
{
People* pre = first;
People* p = first->next;
int cnt = 1;
while (p->next!=p) {
//cout << cnt << endl;
if (cnt < m) {
pre = p;
p = p->next;
cnt++;
}
else {
cout << p->name << " " << p->num << " " << p->gender;
cout << p->age << " " << p->Banji << " " << p->health << endl;
pre->next = p->next;
delete p;
p = pre->next;
cnt = 1;
}
}
cout<< p->name << " " << p->num << " " << p->gender;
cout << p->age << " " << p->Banji << " " << p->health << endl;
first = NULL;
}