Joseph問題解決

一共是8我的-----從第3我的開始-----每過4我的淘汰一人node

代碼完成:code


#include<stdio.h>blog

#include<stdlib.h>io

typedef struct node_t//構建一個單向循環鏈表的結構體循環

{ int number;//數據gc

struct node_t *next; //指向單項列表的結構體im

}*linklist_t,linknode_t;鏈表

int main(int argc, const char *argv[])next

{數據

int n = 8,k = 3,m = 4,i;

linklist_t first =(linklist_t)malloc(sizeof(linknode_t));

linklist_t p=NULL;

first -> number = 1;

first -> next = NULL;

linklist_t new = first; 

for(i = 2;i <= 8;i++ )

{

	linklist_t p =(linklist_t)malloc(sizeof(linknode_t));
	
	new -> next = p;
	
	p -> number = i;
	
	new =new -> next;
	
}

new->next=first;

p = first;

while(--k)//p指向初始位置,即爲3

{

	p = p->next;
	
	while(p->next != p)
	
	{
	
		i=m-1;
		
		while(i--)//p指向位置5
		
		p = p->next;
	
		new = p -> next;

		p->next = new -> next;//刪除操做
		printf("%d --",new->number);
		free(new);
		
	}
	printf("\n%d\n",p->number);

}
return 0;

}

相關文章
相關標籤/搜索