|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
typedef struct node{
int data;
int key;
struct node *next,*pre;
}node;
node *createnc(int n){
node *p =NULL;
node *q =NULL;
node *head,*s;
head=(node*)malloc(sizeof(node));
p=head;
int i;
srand(time(NULL));
i=1;
if(n!=0){
while(i<=n){
s=(node*)malloc(sizeof(node));
s->data=i++;
s->key=rand()%11-5;
while(!s->key){
s->key=rand()%11-5;
}
printf("第%d个人的密码是%d\n",s->data,s->key);
q=p;
p->next=s;
p=s;
p->pre=q;
}
s->next=head->next;
}
free(head);
return s->next;
}
int main(){
srand(time(NULL));
int n=rand()%10+1;
int m=rand()%10+1;
printf("一共有%d人\n",n);
printf("报数上限值是%d\n",m);
int i;
node *p=createnc(n);
node *q;
m%=n;
while(p!=p->next){
if(m>1){
for(i=1;i<m-1;i++){
p=p->next;
}
printf("%d->",p->next->data);
m=p->next->key;
q=p->next;
p->next=q->next;
free(q);
p=p->next;
}
else if(m<0){
m=-m;
for(i=1;i<m;i++){
p=p->pre;
}
printf("%d->",p->pre->data);
m=p->pre->key;
q=p->pre;
p->pre=q->pre;
free(q);
p=p->pre;
}
else if(m==1){
printf("%d->",p->data);
m=p->next->key;
q=p->next;
p->next=q->next;
free(q);
p=p->next;
}
}
printf("%d\n",p->data);
return 0;
} |
|