Chinaunix首页 | 论坛 | 博客
  • 博客访问: 70383
  • 博文数量: 21
  • 博客积分: 1400
  • 博客等级: 上尉
  • 技术积分: 245
  • 用 户 组: 普通用户
  • 注册时间: 2009-06-28 14:14
文章分类

全部博文(21)

文章存档

2011年(1)

2010年(14)

2009年(6)

我的朋友

分类: C/C++

2009-10-13 16:59:09

/*Joseph Circle*/

#include
#include
#include

typedef struct Node
{ int order;
  int password;
  struct Node *next;
}Node,*Linklist;

Linklist init_circle(int n,int s[],Linklist L);
void out_circle(int n,int m,int out[],Linklist L);
void print_out(int n,int out[]);
void exit_circle();

main()
{ char ch;
  Linklist L,q;
  int i,m,n,s[100],out[100],count=0;
  printf("---------------------------------Joseph Circle---------------------------------\n");
  while(1)
  { printf("Please input the total number of persons(n):");
    scanf("%d",&n);
    while(1)
    { if(n>0) break;
      printf("Error!Please enter again(n>0)");
      scanf("%d",&n);
      if(++count==3)  exit_circle();
    }
    printf("Please input everyone's password in order:\n");
    count=0;
    while(1)
    { int ct=0;
      for(i=1;i<=n;i++)
      { scanf("%d",&s[i]);
        if(s[i]<=0)  ct++;
      }
      if(ct==0) break;
      if(++count==3)  exit_circle();
      printf("Error!Please enter again(password>0)\n:");
    }
    printf("Please input the initial value(m):");
    scanf("%d",&m);
    count=0;
    while(1)
    { if(m>0) break;
      printf("Error!Please enter again(m>0)");
      scanf("%d",&m);
      if(++count==3)  exit_circle();
    }
    q=init_circle(n,s,L);
    out_circle(n,m,out,q);
    print_out(n,out);
    printf("\nContinue or Not(Y/N)?");
    getchar();  
    scanf("%c",&ch);  /*此处还有问题!!!!!!*/
    if(!(ch=='y'||ch=='Y'))
        exit_circle();
  }
}

Linklist init_circle(int n,int s[],Linklist L)
{ int i;
  Linklist p,q;
  L=(struct Node*)malloc(sizeof(struct Node));
  L->order=1;
  L->password=s[1];
  q=L;
  for(i=1;i<=n-1;i++)
  { p=(struct Node*)malloc(sizeof(struct Node));
    q->next=p;
    p->order=i+1;
    p->password=s[i+1];
    q=p;
  }
  q->next=L;
  return q;
}

void out_circle(int n,int m,int out[],Linklist r)
{ int i;
  Node *p,*q;
  for(i=1;i<=n;i++)
  { while(m!=1)
    { r=r->next;
      m--;
    }
    p=r->next;
    out[i]=p->order;
    m=p->password;
    q=p->next;
    free(p);
    p=q;
    r->next=p;
  }
}


void print_out(int n,int out[])
{ int i;
  printf("The sequence is:\n");
  for(i=1;i<=n;i++)
    printf("%-7d",out[i]);
}

void exit_circle()
{
    printf("Welcome to use our system!Thank you!");
      //getch();
      exit(0);
}

阅读(516) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~