C language solves the Josephian problem, how should C solve the Josephian problem ?

Updated on technology 2024-02-09
3 answers
  1. Anonymous users2024-02-06

    I wrote it myself and solved it directly with a one-dimensional array.

    #include

    define n 9 total number of people.

    define k 1 to start counting.

    define m 3 intervals.

    Assign a value to the array.

    void setdate(int a,int n)int i;

    for(i=0;i=len) dm=dm-len;

    printf("The last child was %d",a[0]);The last child is placed in a[0].

    main()

    int a[n];

    setdate(a,n);

    play(a,k,m);

  2. Anonymous users2024-02-05

    #include

    #include

    typedef struct node

    int key;

    int data;

    struct node *next;

    node,*linklist;

    void initlist(linklist *l);

    void creat(linklist l,int n);

    void find(linklist l,int n);

    void main()

    linklist l;

    int n;

    printf("Please enter the number of people:");

    scanf("%d",&n);

    initlist(&l);

    printf("Enter each person's password in turn");

    creat(l,n);

    printf("Please enter your initial password");

    find(l,n);

    void initlist(linklist *l)l=(linklist)malloc(sizeof(node));

    l)->next=null;

    void creat(linklist l,int n)node *s,*r;

    int i=1,t;

    r=l;while(i<=n)

    scanf("%d",&t);

    s=(node*)malloc(sizeof(node));

    s->key=t;

    s->data=i;

    r->next=s;

    r=s;i++;

    r->next=l->next;

    void find(linklist l,int n)int n,i=1,t,j=1;

    node *p,*q;

    p=l;scanf("%d",&n);

    t=n;printf("The order of the columns is:");

    while(j<=n)

    while(inext;

    printf("%d ",p->next->data);

    q=p->next;

    t=p->next->key;

    p->next=q->next;

    i=1;j++;

    printf("");

    This is the Joseph loop of the data structure, you see, just modify it...

  3. Anonymous users2024-02-04

    1. To solve this problem, you can use the method of simulating the number to create an array of size n, the nth element of the array indicates whether the nth person is still in the team, first set each element to 1, indicating that all members are in the team. If the nth person is on the team, the nth element is set to 0.

    2. You can use an accumulation counter to simulate the number of reports, which indicates how many people have reported the number of reports in this round, and then iterate through each person, if they are in the team, the counter will be +1, if the number is accumulated to m, then this person is out of the team. And so on until n-1 person comes out of the team, and only 1 person remains.

    3. Finally, iterate through the array and find the people who are still in the team.

Related questions
9 answers2024-02-09

Scope. You static char *chh;

static char *ch1;Although the address pointed to by the two pointers does not change, have you ever wondered whether the memory address they point to has been released, char chc[10]; It's local, the function is out, the lifecycle is over, and you're trying to access it with a pointer in void times(). >>>More

6 answers2024-02-09

Use the ITOA function.

Prototype: extern char *itoa(int i); >>>More

24 answers2024-02-09

Operate from right to left, -i--so calculate -i first--, the operation level is higher, so it can be written as -(i--)i--for 8, add a - sign, so -i--is 8, at this time i--after, i=7, and then -i++ is the same, can be written as -(i++) so -i++ is -7, at this time i++ becomes 8,--i, subtract 1 before execution, so --i is 7, at this time i=7, ++i is the same, add 1 before executing i, so ++i is 8, at this time i=8, then calculate i--,i--is still equal to 8, then i minus one becomes 7, i++, i++ is equal to 7, and then i++, i=8, so finally i=8, and then print it out in turn, pay attention to the operation is from right to left, but print from left to right print i,i++,i--,i,--i,-i++,i-, so the check mark result is out.

16 answers2024-02-09

1) If you are a beginner in software, you can use VC++ on the Internet, and you can go down to the lite version or green version, which is enough. >>>More

12 answers2024-02-09

This problem requires understanding that the system allocates memory to static variables when compiling, and the memory units occupied by them are not released after the function call ends, that is, the value of the variable is the value of the previous function call at the next function call. >>>More