求c语言用循环链表编写约瑟夫环代码(速度求解)!!!
答案:3 悬赏:0 手机版
解决时间 2021-03-14 14:28
- 提问者网友:沦陷
- 2021-03-13 20:51
求c语言用循环链表编写约瑟夫环代码(速度求解)!!!
最佳答案
- 五星知识达人网友:低血压的长颈鹿
- 2021-03-13 21:47
类似差不多的
#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 "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);
}
全部回答
- 1楼网友:你哪知我潦倒为你
- 2021-03-14 00:09
//C++版本的
#include
#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;
}
- 2楼网友:想偏头吻你
- 2021-03-13 23:02
#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++ )
{
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;
}
}
printf( "\n\n" );
}
#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++ )
{
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;
}
}
printf( "\n\n" );
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯