约瑟夫问题-函数功能分解

来源:互联网 发布:软件开发业务员 编辑:程序博客网 时间:2024/05/16 17:40
#include<cstdlib>#include<iostream>using namespace std;struct Node {int num;Node *next;};Node *Lcreat(int num){int cnt = 0;Node *h = NULL;Node *pr = new Node;h = pr;h->num = num;while(--num) {Node *p = new Node;p->num = num;p->next = h;h = p;}pr->next = h;return pr;}int findJos(int num, int span){Node *p = Lcreat(num);for(int i = 1; i < num; ++i){for(int j = 1; j < span; ++j)p = p->next;Node *tp = p->next;p->next = tp->next;cout << tp->num << ' ';delete tp;}int t = p->num;cout << p->num << endl;delete p;return t;}int main(void){int m, n;while(cin >> m >> n)cout << "The last one is No." << findJos(m, n) << endl;system("pause");return 0;}
建立链表->模拟操作->输出结果.
0 0
原创粉丝点击