您的位置:首页 > 编程语言 > C语言/C++

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

2015-03-21 20:25 288 查看
#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;
}
建立链表->模拟操作->输出结果.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ 链表 约瑟夫问题
相关文章推荐