魔术师发牌问题

就是有一堆牌,先跳过零张,翻出第一张为一,然后跳过一张,翻出第二张为2。。。。

被跳过的牌放在最下面。。。。

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

void circle_init(Node *head, const int len);
void circle_set(Node *head, const int len); 
void circle_print(Node *head);
void circle_des(Node *head);

int main()
{
    int len;
    int num;
    Node head = {0, &head};
    
    printf("输入牌的张数:");
    scanf("%d", &len);
    
    circle_init(&head, len); 

    circle_set(&head, len);
    
    circle_print(&head);
    circle_des(&head);
    return 0;
}

void circle_init(Node *head, const int len)
{
    int i = 0;
    Node *p = head;
    
    for(i=0; i<len; i++)
    {
        p->next = (Node *)malloc(sizeof(Node));
        p = p->next;
        p->data = 0;
        p->next = head->next;
    }
    
}

void circle_set(Node *head, const int len)
{
    Node *p = head->next;
    int i = 0;
    int left = len;
    
    p->data = 1;
    left -= 1;
    
    while(left != 0)
    {
        for(i=1; i<=len-left+1; i++)
        {
            while(p->next->data != 0)
            {
                p = p->next;
            }
            p = p->next;
        }
        
        p->data = len-left + 1;
        left -= 1;
    }
}

void circle_print(Node *head)
{
    Node *p = head->next;
    printf("链表:\n");
    
    do {
        printf("%d ", p->data);
        p = p->next;
    }while(p != head->next);
    
    printf("\n");
}

void circle_des(Node *head)
{
    Node *p = head->next;
    Node *temp = NULL;
    
    
    do {
        temp = p->next;
        free(p);
        p = temp;
    }while(p->next != head->next);
    free(p);
    head->next = head;
}

 

顺便感叹一下吧,小学大约三年级的时候玩这个游戏,是每次都固定跳过一张,翻出第二张。。。。被要求给十三张牌排好顺序,让翻出来依次为 1,2,3,4。。。。

我就想说,如果回到小学三年级,我会用三列A-K的牌;

第一列按照从1,2,3,。。。顺序排开:

1,2,3,4,5,6,7,8,9,10,11,12,13

第二列,直接从1,2,。。13顺序排成一叠,然后,按照游戏规则,跳过一张,翻出一张,将翻出的牌依次牌好:

2,4,6,8,10,。。。

将第一列牌中第二列的牌分别替换成它对应的牌,啊,有点绕:

简单点说,就是2是第一个被翻出来的,但是我们期望它是1,所以,将第一列牌中间的2所在的位置,换成1;4所在的位置换成2;6所在的位置换成3.。。。。。

额。。。。没听懂就当我没说吧。。。。。

 

posted @ 2017-08-12 15:35  牛奶无花果  阅读(709)  评论(0编辑  收藏  举报