题解 | 约瑟夫环 链表

约瑟夫环

https://www.nowcoder.com/practice/e417cfe32c74416ca38247f619ddb322

#include <stdio.h>
#include<stdlib.h>
typedef struct Node{
    int n;
    struct Node*next;}
    Node;
int main() {
    int a, b,c;
    while (scanf("%d %d %d", &a, &b,&c) != EOF) { // 注意 while 处理多个 case
        // 64 位输出请用 printf("%lld") to
        Node*h=NULL;
        Node*t=NULL;
        Node*p=NULL;
        for(int i=0;i<a;i++){
            p=(Node*)malloc(sizeof(Node));    
        p->n=i;
        p->next=NULL;
        if(h==NULL){
            h=p;
            t=p;
        }else{
            t->next=p;
            t=p;
        }}
        t->next=h;
        Node*cu=h;
        while(cu->n!=b) cu=cu->next;


        while(cu->next!=cu){
            for(int i=0;i<c-2;i++) cu=cu->next;
        Node*oNode=cu->next;
        cu->next=oNode->next;
        free(oNode);
        cu=cu->next;}
        printf("%d",cu->n);
            
        
        
    }
    return 0;
}

全部评论

相关推荐

12-20 11:26
复旦大学 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务