类似差不多的
#include "stdio.h"
#include "stdlib.h"
#define S sizeof(struct node)
struct node
{
int num;
struct node *next;
};
typedef struct node NODE;
NODE *createlinklist(int n)
{
NODE *head,*p,*q;
int i=1;
head=p=(struct node*)malloc(sizeof(struct node));
p->num=i;
for(i=2;i<=n;i++)
{
q=(struct node*)malloc(sizeof(struct node));
if(q==0) return(0);
p->next=q;
p=q;
p->num=i;
}
p->next=head; /*使链表尾指向链表头 形成循环链表*/
return head;
}
void printlinklist(NODE *p,int n)
{
int i;
NODE *q = p;
if(NULL == q->next){
printf("the list is NULL!");
return;
}
printf("所有玩家的信息列表:\n");
for(i=1;i<=n;i++)
{
if(NULL == q){
printf("the list is NULL!");
return;
}
printf("%d ",p->num);
p=p->next;
}
printf("\n");
}
void joseph(NODE *p,int n,int m)
{
int i,j;
NODE *q;
for(i=1;i
for(j=1;j<=m-1;j++)
{
p=p->next;
}
q=p->next;
p->next = q->next;
printf("%d ",q->num);
free(q);
}
printf("\n最后剩余的是第%d号.\n",p->num);
p->next=NULL;
}
void main()
{
NODE *head;
int n,m;
printf("请输入人数N:\n");
scanf("%d",&n);
printf("输入K:\n");
scanf("%d",&m);
head=createlinklist(n);
printlinklist(head,n);
printf("依次被选出的是:\n");
joseph(head,n,m);
}
#include
#define size 100 /* 输入人数的上限 */
void main()
{
int person[size];
int i, j; /* 循环修正变量 */
int arrayLen; /* 数组长度 */
int start, overNum; /* 开始位置各跨过位置 */
int deleNum; /* 出列人所在数组中的下标 */
int name, total; /* 输入时,人的信息以及人的总数 */
printf( "请输入圆桌上人的总数: " );
scanf( "%d", &arrayLen ); printf( "\n" );
if( ( arrayLen > size ) || ( arrayLen < 0 ) )
{
printf( "超出范围,请重新输入: " );
scanf( "%d", &arrayLen ); printf( "\n" );
};
printf( "请输入各个人的信息(整数): \n" );
for( i = 0; i < arrayLen; i++ )
{
scanf( "%d", &name );
person[i] = name;
}
printf( "你输入的数据的顺序为: \n" );
for( i = 0; i < arrayLen - 1; i++ )
printf( " %d ==>", person[i] );
printf( "%d \n", person[arrayLen - 1] );
printf( "你打算从第几个人开始? 请输入开始号: " );
scanf( "%d", &start );
printf( "\n" );
start = start - 1;
printf( "请输入相邻两出列人之间的间隔: " );
scanf( "%d", &overNum );
printf( "\n" );
total = arrayLen;
printf( "程序运行后,出列人的顺序为:\n\n" );
for( i = 0; i < total; i++ ) /* 要打印total个人的情况,故做total次 */
{
if ( arrayLen == 1 )
printf( "%d", person[0] ); /* 如果是数组只剩一个元素,直接出列 */
else
{
deleNum = ( start + overNum - 1 ) % arrayLen; /* 此取模保证循环 */
printf( "%d ==> ", person[deleNum] );
for ( j = deleNum; j < arrayLen; j++ ) /* 将出列元素后面的各元素前移 */
person[j] = person[j+1];
start = deleNum;
arrayLen = arrayLen - 1; /* 移动完毕后,数组长度减1 */
}
}
printf( "\n\n" );
}
//C++版本的
#include
#include
#include
int main()
{
std::vector
int m=3, n=7, k=2;
for (int i=0; i
intList.push_back(i+1);
}
std::vector
{
it = intList.begin();
for (int i=0; i
++it;
if (it == intList.end())
{
it = intList.begin();
}
}
}
while ( !intList.empty() )
{
for (int i=0; i
++it;
if (it == intList.end())
{
it = intList.begin();
}
}
int current = *it;
std::cout << current << std::endl;
m = it - intList.begin();
intList.erase(it);
it = intList.begin();
for (int i=0; i
++it;
if (it == intList.end())
{
it = intList.begin();
}
}
}
return 0;
}
类似差不多的
#include
"stdio.h"
#include
"stdlib.h"
#define
S
sizeof(struct
node)
struct
node
{
int
num;
struct
node
*next;
};
typedef
struct
node
NODE;
NODE
*createlinklist(int
n)
{
NODE
*head,*p,*q;
int
i=1;
head=p=(struct
node*)malloc(sizeof(struct
node));
p->num=i;
for(i=2;i<=n;i++)
{
q=(struct
node*)malloc(sizeof(struct
node));
if(q==0)
return(0);
p->next=q;
p=q;
p->num=i;
}
p->next=head;
/*使链表尾指向链表头
形成循环链表*/
return
head;
}
void
printlinklist(NODE
*p,int
n)
{
int
i;
NODE
*q
=
p;
if(NULL
==
q->next){
printf("the
list
is
NULL!");
return;
}
printf("所有玩家的信息列表:\n");
for(i=1;i<=n;i++)
{
if(NULL
==
q){
printf("the
list
is
NULL!");
return;
}
printf("%d
",p->num);
p=p->next;
}
printf("\n");
}
void
joseph(NODE
*p,int
n,int
m)
{
int
i,j;
NODE
*q;
for(i=1;i
for(j=1;j<=m-1;j++)
{
p=p->next;
}
q=p->next;
p->next
=
q->next;
printf("%d
",q->num);
free(q);
}
printf("\n最后剩余的是第%d号.\n",p->num);
p->next=NULL;
}
void
main()
{
NODE
*head;
int
n,m;
printf("请输入人数N:\n");
scanf("%d",&n);
printf("输入K:\n");
scanf("%d",&m);
head=createlinklist(n);
printlinklist(head,n);
printf("依次被选出的是:\n");
joseph(head,n,m);
}