002 -- Circle LinkList_Josephus Story

Circle List puzzle

Josephus Story:

there is 41 people , 39 of them have the agreement that: all these 41 people stand as a circle, and one of them count from 1, and the one count to 3, suicide ! and the people next to him count from 1 again, untill all 41 people are dead. But Josephus and his friend don‘t want to die, so they have their location arranged, and become the last 2 person for counting.

// give a number m, n people as a circle and start say a number with 1, the one with number m to say, get out of the circle 

#include <stdio.h>
#include <stdlib.h>

typedef struct node
{
    int data;
    struct node *next;
}node;

node *create(int n)
{
    node *p = NULL, *head;
    head = (node*)malloc(sizeof(node));
    p = head;
    node *s;
    int i = 1;

    if(0!n)
    {
        while(i<=n)
        {
            s = (node*)malloc(sizeof(node));
            s->data=i++; //initiate the circle list, the first node is 1; the secode node is 2
            p->next = s;
            p=s;
        }
        s->next = head->next;

    }

    free(head);
    return s->next;
}

int main()
{
    int n = 41; //total 41 people_initiate a list with 41 nodes
    int m = 3; //who count to 3, out of circle _delete the node
    int i;
    node *p = create(n);
    node *temp;

    m = n%m; //m=2

    while(p!=p->next) //if p = p->next, means only 1 node left in the list
    {
        for (i=1;i<m-1;i++)
        {
            p=p->next; //now p come to the node before the deleting node, only run 1 time
        }

        printf("%d", p->next->data); //print the value of the node who going to be deleted

        temp = p->next;
        p->next = temp->next;
        free(temp);

        p=p->next; // now p point to the node who will start count with 1

    }

    printf("%d\n",p->data);  //print the last node of the list 

    return 0;
}
时间: 2024-10-08 13:27:08

002 -- Circle LinkList_Josephus Story的相关文章

002 -- Circle LinkList 2 -- connect 2 circle link lists

The operation fullfilled codes: // A, B is the rear for 2 LinkList with nodes LinkList Connect(LinkList A,LinkList B) { p = A->next; // use p to store A list head A->next = B->next->next; // A change to point to b1 as the next. free(B->next

002 -- Circle LinkList 3 -- Puzzels_Magic Poker and Latin

000--Magic Poker Put the poker with specific sequence, so that they can show as below: count 1, open the first card as A, count 2, the first one place to bottom and open the second one as 2; for 3, the first 2 cards put to the bottom, and open the th

Mathametica_00_The Hertz&#39; dipoles

Mathematica_00_画出赫兹偶极子的电场.磁场.坡印廷矢量的图像随时间的变化 Manipulate[ Pane[Module[{gg, inset1, inset2}, inset1 = Inset[Graphics[ Text[Style["\!\(\*SubscriptBox[\(p\), \(0\)]\)", 20, White]]], {0, 2.5}]; inset2 = Inset[Graphics[ Text[Style[ Row[{Subscript[Styl

657. Judge Route Circle 判断线路成圆

Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot makes a circle, which means it moves back to the original place. The move sequence is represented by a string. And each move is represent by a characte

html入门之002

html实体 &nbsp 空格 &lt < &gt > &quot 引用 &copy 版权 &times X &divide ÷ &yen ¥ 列表标签 ul 属性type(disc(默认/square/circle) li 列表项 属性type值同上 ol 有序列表 属性 reversed(降序) type(1/a/A/I) start(值数字) dl 定义列表 dt 列表项目的标题 列表项目的描述 文本标签 *em 强调 表现

SVG圆形&lt;circle&gt; 标签

1 <?xml version="1.0" standalone="no"?> 2 <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" 3 "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> 4 5 <svg width="100%" height="100%" v

hzau 1208 Color Circle(dfs)

1208: Color Circle Time Limit: 1 Sec  Memory Limit: 1280 MBSubmit: 289  Solved: 85[Submit][Status][Web Board] Description There are colorful flowers in the parterre in front of the door of college and form many beautiful patterns. Now, you want to fi

java定义一个Circle类,包含一个double型的radius属性代表圆的半径,一个findArea()方法返回圆的面积

需求如下:(1)定义一个Circle类,包含一个double型的radius属性代表圆的半径,一个findArea()方法返回圆的面积. (2)定义一个类PassObject,在类中定义一个方法printAreas(),该方法的定义如下: public void printAreas(Cirlce c, int times) 在printAreas方法中打印输出1到time之间的每个整数半径值,以及对应的面积.例如,times为5,则输出半径1,2,3,4,5,以及对应的圆面积. 在main方法

HDU 5343 MZL&#39;s Circle Zhou

MZL's Circle Zhou Time Limit: 1000ms Memory Limit: 131072KB This problem will be judged on HDU. Original ID: 534364-bit integer IO format: %I64d      Java class name: Main MZL's Circle Zhou is good at solving some counting problems. One day, he comes