我怎么这么菜


私信TA

用户名:xujingcheng

访问量:16827

签 名:

Break Away

等  级
排  名 718
经  验 3756
参赛次数 4
文章发表 44
年  龄 10
在职情况 学生
学  校 NUAA
专  业

  自我简介:

毕业前学一下编程, 嗯! 是这样。

#include<stdio.h>
#include"malloc.h"
typedef struct node
{
    int num;
    struct node *next;
}*Node;
Node creat(int n);
void get_result(int n);
int main()
{
    int n;
    scanf("%d",&n);
    get_result(n);
    return 0;
}
Node creat(int n)
{
    int i=1;
    Node head=(Node)malloc(sizeof(struct node));
    head->next=NULL;
    Node p=head;
    Node q;
    for(;i<=n;i++)
    {
        q=(Node)malloc(sizeof(struct node));
        q->num=i;
        q->next=p->next;
        p->next=q;
        p=q;
    }
    p->next=head->next;
    return head;
 }
void get_result(int n)
{
    Node head,p,q;
    head=creat(n);
    p=head->next;
    free(head);
    while(p->next!=p)
    {
        p=p->next;
        q=p->next;
        p->next=q->next;
        free(q);
        p=p->next;
    }
    /***************************
    这里同样可以循环n-1次,因为最后圈中只剩下1个人
    int j;
    for(j=0;j<n-1;j++)
    {
        p=p->next;
        q=p->next;
        p->next=q->next;
        free(q);
        p=p->next;
    }
    ****************************/
    printf("%d\n",p->num);
    free(p);
    return ;
}
 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区