约瑟夫环问题
来源:互联网 发布:每日红包软件靠谱么 编辑:程序博客网 时间:2024/06/03 17:34
用户输入M,N值,从1至N开始顺序循环数数,每数到M输出该数值,直至全部输出。
#include <iostream>using namespace std;struct node{int data;node *next;};void createList(node *&head, node *&tail, int n){if(n < 1){head = NULL;return;}head = new node();head->data = 1;head->next = NULL;node *p = head;for(int i = 2; i <= n; ++i){p->next = new node();p = p->next;p->data = i;p->next = NULL;}tail = p;p->next = head;}void Print(node *head){node *p = head;while(p && p->next != head){cout << p->data << " ";p = p->next;}if(p)cout << p->data << endl;}void countPrint(node *&head, node *&tail, int m){node *cur = head;node *pre = tail;int cnt = m - 1;while(cur && cur != cur->next){if(cnt){cnt--;pre = cur;cur = cur->next;}else{pre->next = cur->next;cout << cur->data << " ";delete cur;cur = pre->next;cnt = m - 1;}}if(cur){cout << cur->data << endl;delete cur;head = tail = NULL;}}int main(){node *head, *tail;int n, m;cin >> n >> m;createList(head, tail, n);Print(head);countPrint(head, tail, m);return 0;}
0 0
- 约瑟夫问题、约瑟夫环
- Josephus约瑟夫环问题
- 约瑟夫环问题
- 约瑟夫环问题(Josephus)
- 约瑟夫环问题--java
- 约瑟夫环问题 Josephus
- 约瑟夫环问题
- 约瑟夫环问题
- 约瑟夫环问题
- 约瑟夫环问题
- 约瑟夫环问题
- 求解约瑟夫环问题
- 约瑟夫环问题
- 约瑟夫环问题
- 约瑟夫环问题
- 约瑟夫环问题
- 约瑟夫环问题
- 约瑟夫环问题
- 浏览器(safari,Chrome,FireFox)进网站乱跳其他网站问题
- ByteBuffer用法小结
- 在C#的Web项目中调用Matlab代码的方法
- svn 常用指令
- const类型指针与普通指针相互赋值
- 约瑟夫环问题
- 外键约束
- ARC,block,protocol,代理设计模式
- 数据库分库分表介绍
- 已成功与服务器建立连接 但是在登录过程中发生错误。 provider 共享内存提供程序 error 0 管道的另一端上无任何进程。
- Problem E.转化最大数
- 对于网上购票系统12306,如果你是架构师,你会怎么办?-论坛精彩回复整理
- 一些ADO数据库入门例子与资料
- 老湿人----山河拱手,为君一笑