零韵苍虬吧 关注:38贴子:774
  • 4回复贴,共1
#ifndef LNODE_H
#define LNODE_H
#define ERROR 0
#define OK 1
#define MAX 1000
int Key[MAX];
int n;
typedef struct LNode{
int keyword, number;
struct LNode *next;
}LNode, *LinkList;
void JosephC(LinkList p, int m, int x){
LinkList q;
int i;
if(x != 0){
q = p;
m %= x;
if(m == 0) m = x;
for(i = 1; i <= m; i++){
p = q;
q = p -> next;
}
p -> next = q -> next;
i = q -> keyword;
printf("%d ", q -> number + 1);
free(q);
JosephC(p, i, x-1);
}
}
#endif
#include<stdio.h>
#include<stdlib.h>
#include"LNode.h"
int main(){
int i, m, j;
LinkList Lhead, p, q;
printf("输入初值m:\n");
scanf("%d", &m);
printf("输入人数n:\n");
scanf("%d", &n);
printf("依次输入%d个人的密码:\n", n);
for(j = 0; j < n; j++){
scanf("%d", Key + j);
}
Lhead = (LinkList)malloc(sizeof(LNode));
if(!Lhead)
return ERROR;
Lhead -> keyword = Key[0];
Lhead -> next = NULL;
Lhead -> number = 0;
p = Lhead; //初始化头结点
for(i = 1; i < n; i++){
if(!(q = (LinkList)malloc(sizeof(LNode))))
return ERROR;
q -> keyword = Key[i];
q -> number = i;
p -> next = q;
p = q;
}
p -> next = Lhead;
printf("其出列顺序为:\n");
JosephC(p, m, n);
printf("\n");
return OK;
}


IP属地:浙江1楼2016-10-10 11:18回复
    #ifndef LNODE_H
    #define LNODE_H
    #define ERROR 0
    #define OK 1
    #define MAX 30
    int Key[MAX];
    int n;
    typedef struct LNode{
    int keyword, number;
    struct LNode *next;
    }LNode, *LinkList;
    void JosephC(LinkList p, int m, int x){
    LinkList q;
    int i;
    if(x != 0){
    q = p;
    m %= x;
    if(m == 0) m = x;
    for(i = 1; i <= m; i++){
    p = q;
    q = p -> next;
    }
    p -> next = q -> next;
    i = q -> keyword;
    printf("%d ", q -> number + 1);
    free(q);
    JosephC(p, i, x-1);
    }
    }
    #endif
    #include<stdio.h>
    #include<stdlib.h>
    #include"LNode.h"
    int main(){
    int i, m, j;
    LinkList Lhead, p, q;
    printf("输入初值m:\n");
    scanf("%d", &m);
    printf("输入人数n:\n");
    scanf("%d", &n);
    printf("依次输入%d个人的密码:\n", n);
    for(j = 0; j < n; j++){
    scanf("%d", Key + j);
    }
    Lhead = (LinkList)malloc(sizeof(LNode));
    if(!Lhead)
    return ERROR;
    Lhead -> keyword = Key[0];
    Lhead -> next = NULL;
    Lhead -> number = 0;
    p = Lhead; //初始化头结点
    for(i = 1; i < n; i++){
    if(!(q = (LinkList)malloc(sizeof(LNode))))
    return ERROR;
    q -> keyword = Key[i];
    q -> number = i;
    p -> next = q;
    p = q;
    }//依次初始化其他节点
    p -> next = Lhead;//形成循环链表
    printf("其出列顺序为:\n");
    JosephC(p, m, n);
    printf("\n");
    return OK;
    }


    IP属地:浙江2楼2016-10-10 11:42
    回复
      #include<stdio.h>
      #include <stdlib.h>
      typedef struct LNode
      {
      int num;
      int password;
      struct LNode *next;
      }LinkList;
      LinkList *creat(int n)
      {
      LinkList *p,*q,*head;
      int i=1;
      head=p=(LinkList *)malloc(sizeof(LinkList));
      p->num=i;
      printf("输入第1个人的密码:");
      scanf("%d",&p->password);
      for(i=2;i<=n;i++)
      {
      q=(LinkList *)malloc(sizeof(LinkList));
      if(q==0) return(0);
      printf("输入第%d个人的密码:",i);
      scanf("%d",&q->password);
      q->num=i;
      p->next=q;
      p=q;
      }
      p->next=head;
      return head;
      }
      void Joseph(LinkList *L, int n)
      {
      int m,i;
      LinkList *p=L,*q,*s;
      printf("输入m的初值:");
      scanf("%d",&m);
      m%=n;
      printf("出列顺序为:");
      while(p->next!=p)
      {
      for(i=1;i<m;i++)
      {
      q=p;
      p=p->next;
      }
      printf("%d",p->num);
      m=p->password;
      s=p;
      q->next=p->next;
      p=p->next;
      free(s);
      }
      printf("%d",p->num);
      printf("\n");
      }
      #include<stdio.h>
      #include <stdlib.h>
      #include "LNode.h"
      void main()
      {
      LinkList *L;
      int n;
      printf("请输入实验人数:");
      scanf("%d",&n);
      L=creat(n);
      Joseph(L,n);
      }


      IP属地:浙江3楼2016-10-23 22:46
      收起回复