直接用链表模拟就可以了



#include <stdio.h>

#include<string.h>
#include<stdlib.h>
#include<stdbool.h>

typedef struct Node{
    int val;
    struct Node *next;

}*List;
int main() {
    bool c;
    int a, b,n,j,i;
    while(scanf("%d",&n)!=EOF){
        List node=(List)malloc(sizeof (List));
        node->val=0;
        node->next=NULL;
        List nd1=node;

        for(i=1;i<n;i++){
            List nd2=(List)malloc(sizeof(List));
            nd2->val=i;nd2->next=NULL;
            nd1->next=nd2;
            nd1=nd2;
        }
        nd1->next=node;
        nd1=node;
        j=0;
        int res=0;
        while(j<n){
            //printf("%d ",nd1->val);
            List nd2=nd1->next->next;
            res=nd2->val;
            nd1->next->next=nd2->next;
            nd1=nd2->next;
            j++;
        }
        printf("%d\n",res);
    }
    
    
    return 0;
}