链表 - 约瑟夫问题
生活随笔
收集整理的這篇文章主要介紹了
链表 - 约瑟夫问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
就是簡單的鏈表操作,下面是實例。
#include<iostream> #include<cstring> #include<cstdio> #include<cstdlib> #include<cmath> #include<algorithm> using namespace std; int tot; struct node {int num;node() {num = ++tot;}node *next,*past; }; int main() {int m,n;while(1) {scanf("%d%d",&n,&m);tot = 0;node *head = new node,*temp = head;node *tmp = head;for(int i = 1; i<n; i++) {temp -> next = new node;temp -> next -> past = temp;temp = temp -> next;}head -> past = temp;temp -> next = head;if(!m && !n) {break;}int to = 0;while(tmp -> next != tmp) {to ++;if(!(to % m)) {node *t = tmp;tmp -> past -> next = tmp -> next;tmp -> next -> past = tmp -> past;tmp = tmp -> next;delete t;} else {tmp = tmp -> next;}}printf("%d\n",tmp -> num);delete tmp;}return 0; }轉載于:https://www.cnblogs.com/wenoi/p/9093938.html
總結
以上是生活随笔為你收集整理的链表 - 约瑟夫问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python time模块
- 下一篇: 块存储、文件存储、对象存储意义及差异