您的位置:首页 > 理论基础 > 数据结构算法

[Coursera]数据结构基础_Week2_线性表_Q1

2015-12-21 14:25 337 查看
#include<iostream>
#include<string>
#include<string.h>
using namespace std;
struct Monkey {
int n;
Monkey* next = NULL;
Monkey* prev = NULL;
Monkey(int n_):n(n_){}
};
int main()
{
int n, m;
cin >> n >> m;
Monkey* head = new Monkey(1);
Monkey* p = head;
for (int i = 2; i <= n; i++) {
p->next= new Monkey(i);
p->next->prev = p;
p = p->next;
}
p->next = head;
head->prev = p;
p = p->next;
int left = n;
while (left > 1) {
for (int i = 1; i < m; i++) {
p = p->next;
}
p->next->prev = p->prev;
p->prev->next = p->next;
Monkey* temp = p;
p = p->next;
delete temp;
left--;
}
cout << p->n << endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: