[数据结构]Joseph problem(模拟)
2016-03-30 18:18
309 查看
#include<iostream> using namespace std; struct Node { Node(); Node(int item, Node *add_on = NULL); Node *next; int entry; }; Node::Node() { next = NULL; } Node::Node(int item, Node * add_on) { entry = item; next = add_on; } void main() { while (1) { int n, m, k; cout << "please enter the num of the people." << endl; cin >> n; cout << "please enter the ID of people to begin." << endl; cin >> k; cout << "please enter the count to exit." << endl; cin >> m; cout << "leaving order:"; Node *head = new Node(1); Node *p = head; for (int i = 2; i <= n; i++) {//生成链表 p->next = new Node(i); p = p->next; } p->next = head; for (int i = 1; i < k; i++, p = p->next); while (n != 1) { for (int i = 0; i < m - 1; i++, p = p->next); Node *temp = p->next; p->next = temp->next; cout << temp->entry << ' '; delete temp; n--; } cout << endl<<"the winner is " << p->entry << endl; } }
相关文章推荐
- [数据结构]Linked_stack
- [数据结构]Linked_queue
- 《数据结构》 队列(Queue)操作代码集合
- 数据结构 - 简单的单链表结构
- 【暑假】[实用数据结构] AC自动机
- 【暑假】[实用数据结构]UVAlive 3942 Remember the Word
- 【暑假】[实用数据结构]UVAlive 3026 Period
- 【暑假】[实用数据结构]UVAlive 4670 Dominating Patterns
- 【暑假】[基本数据结构]根据BFS与DFS确定树
- 【暑假】[基本数据结构]根据in_order与post_order构树
- 【暑假】[基本数据结构]基本的数据结构知识点总结梳理
- 【暑假】[实用数据结构]动态连续和查询问题
- 【暑假】[实用数据结构]范围最小值问题(RMQ)
- 【暑假】[实用数据结构]动态范围查询问题
- 【暑假】[实用数据结构]UVa11995 I Can Guess the Data Structure!
- 【暑假】[实用数据结构]UVa11991 Easy Problem from Rujia Liu?
- 【暑假】[实用数据结构]UVAlive 3135 Argus
- 【暑假】[实用数据结构]UVa11997 K Smallest Sums
- 【暑假】[实用数据结构]UVAlive 3644 X-Plosives
- 【暑假】[实用数据结构]UVAlive 3027 Corporative Network