當前位置:編程學習大全網 - 編程軟體 - 約瑟夫環問題怎麽解決啊?請用C語言寫代碼,謝謝!

約瑟夫環問題怎麽解決啊?請用C語言寫代碼,謝謝!

#include"MyNode.h" //文件1

Node::Node( )

{

next = NULL;

}

Node::Node(Node_entry item, Node *add_on)

{

entry = item;

next = add_on;

}

--------------

#include<iostream.h> //文件2

typedef int Node_entry;

struct Node {

// data members

Node_entry entry;

Node *next;

// constructors

Node( );

Node(Node_entry item, Node *add_on = NULL);

};

---------------------

#include"MyNode.h" //主程序

#include<iostream.h>

void main(){

int n,m;

Node *p=NULL, *p_head=NULL ,*p_tmp=NULL;

cout<<"This is a YSF(n,m) problem."<<endl;

//n people, m count, both n and m should >= 1

do{

cout<<"Please input the n(n>1).";

cin>>n;

}while(n<1);

do{

cout<<"Please input the m(m>1).";

cin>>m;

}while(m<1);

//construct the circular link structure

for(int i=n;i>=1;i--){

p_head = new Node(i,p);

p=p_head;

}

//get to the tail, which is n.

while(p->next)p=p->next;

//connect to a circular

p->next=p_head;

//go with the circular

for(i=1;i<n;i++){ //do n-1 times to get rid of n-1 elements.

for(int k=1;k<m;k++)p=p->next;

p_tmp=p->next;

p->next=p_tmp->next;

cout<<endl<<"Get rid of:"<<p_tmp->entry;

delete p_tmp;

}

//Print the left element

cout<<endl<<"The left element is:"<<p->entry<<endl;

}

我這個學期也搞數據結構

程序可以運行的

  • 上一篇:新捷達abs控制單元的代碼是什麽?
  • 下一篇:計算機中的“01”代碼是什麽意思?
  • copyright 2024編程學習大全網