首页 > 学院 > 开发设计 > 正文

约瑟夫环问题

2019-11-06 06:37:13
字体:
来源:转载
供稿:网友

问题描述:http://www.sdutacm.org/onlinejudge2/index.php/Home/Index/PRoblemdetail/pid/1197.html

#include <iostream>#include <malloc.h>using namespace std;typedef struct Node{ int data; struct Node* next;}Node;int main (){ int n,m,count,length; cin>>n>>m; //建表前的初始化 Node *head = (Node*)malloc(sizeof(Node)); head->data = 1; head->next = NULL; Node *tail = head; for( int i=2;i<=n;++i ) { Node *p = (Node*)malloc(sizeof(Node)); p->data = i; p->next = NULL; tail->next = p; tail = p; } tail->next = head; //构成一个环 //游戏前的初始化 count = 1; length = n; Node *p = head; while( length != 1 ) { if( count == m-1 ) //代表下一个节点为要删除的节点 { // cout<<"will delete:"<<p->next->data<<endl; p->next = p->next->next; //删除节点 count = 1; p = p->next; length--; } else { p = p->next; count++; } } cout<<p->data<<endl; return 0;}

上一篇:344. Reverse String

下一篇:Form表单传值

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表