我怎么这么菜


私信TA

用户名:xujingcheng

访问量:16915

签 名:

Break Away

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

  自我简介:

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

解题思路:
 /*******************首先构建循环链表,然后删除n-1个计数为m的节点******************/

#include<stdio.h>
#include<malloc.h>
typedef struct node
{
    int num;
    struct node *next;
    }*Node;
int main()
{
    int func(int n,int m);
    int m,n,number;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
     number=func(n,m);
      printf("%d\n",number);
    }
    return 0;
}
int func(int n,int m)
{
    Node creat(int n);
    /*首先要找到即将删除节点的位置*/
    Node head,p,temp;
    head=creat(n);
    p=head->next;
    free(head);
    int j,k=1,r;
    for(;k<=n-1;k++)
    {
      j=2;
    while(m-j)
    {
      p=p->next;
      j++;
    }
    temp=p->next;
    p->next=temp->next;
    p=p->next;
    free(temp);
    }
    r=p->num;
    free(p);
    return r;
}
Node creat(int n)
{
    Node head=(Node)malloc(sizeof(struct node));
    head->next=NULL;
    Node q=head;
    Node p;
    int i;
    for(i=1;i<=n;i++)
    {
        p=(Node)malloc(sizeof(struct node));
        p->num=i;
        p->next=q->next;
        q->next=p;
        q=p;
    }
    q->next=head->next;
       return head;
}





    

      


 

 

0.0分

0 人评分

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

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区