约瑟夫问题 c++代码

来源:互联网 发布:淘宝口令变成链接 编辑:程序博客网 时间:2024/06/08 13:18
#include
using namespace std;
struct node
{
    int data;
    node*next;
};

class ysf
{
public:
      ysf(int *a,int n);
void func(int n,int m);
private:
node *p;
node *head;
};

 ysf::ysf(int *a,int n)
{     p=head=new node;
     for(int i=0;i
{
        p->data=*(a+i);
        p->next=new node;
        p=p->next;
}
     p->data=*(a+n-1);
     p->next=head;

}
void ysf::func(int n,int m)
{
      p=head;
     for(int k=1;k<=n;k++)
{
 
         for(int j=1;j
{
           while(p->data==0)
  {
     p=p->next;
  }
           p=p->next;
}
       while(p->data==0)
       {
 p=p->next;
  }
       cout<<p->data<<endl;
       p->data=0;
  p=p->next;
}

}

int main()
{
int m,n;
int a[100];
cout<<"input:"<<endl;
cout<<"n=";
cin>>n;
cout<<"m=";
cin>>m;
cout<<"please the numbers:"<<endl;
for(int ii=0;ii
cin>>*(a+ii); 
 
ysf obj(a,n); 
 obj.func(n,m);

return 0;
}

0 0
原创粉丝点击