当前位置:编程学习 > C#/ASP.NET >>

用C++编写约瑟夫环(第二次修改)超精简


#include<stdlib.h>
#include<iostream.h>
typedef struct LNode
{int num,pwd;
struct LNode *next;
}LNode, *LinkList;
void main()
{
 int a,i=1,m,n,j;//m为报数上限值,n为人数,j为输入的密码;
    cout<<"please enter m:";
    cin>>m;
    cout<<"please enter n:";
    cin>>n;
    LinkList head,p,pt;
 while(i<=n)
    {
  pt=new LNode;
  if(i==1)
  {
   p=head=pt;
      cout<<"please enter the password:";
   cin>>j;
         p->num=i;
         p->pwd=j;
   i++;
  }else
  {
   p->next=pt;
      p=pt;
            cout<<"please enter the password:";
   cin>>j;
         p->num=i;
         p->pwd=j;
         i++;
  }
 }p->next=head;
    for(i=1;i<=n;i++)
 {
  for(a=1;a<m;a++)
  p=p->next;
     pt=p->next;
     m=pt->pwd;
     cout<<pt->num<<'n';
  p->next=pt->next;
  free(pt);
 }
}

<
补充:asp.net教程,C语言
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,