永发信息网

13个人围成一圈,从第一个人开始顺序报号1、2、3,凡是报到3的人退出圈子。最后留的人是几号?(链表)

答案:2  悬赏:70  手机版
解决时间 2021-11-17 17:29
  • 提问者网友:凉末
  • 2021-11-16 16:59
13个人围成一圈,从第一个人开始顺序报号1、2、3,凡是报到3的人退出圈子。最后留的人是几号?(链表)
最佳答案
  • 五星知识达人网友:野味小生
  • 2021-11-16 17:41
#include "stdio.h"
#include "conio.h"
#define nmax 13
main()
{
int i,k,m,n,num[nmax],*p;
printf("please input the total of numbers:");
scanf("%d",&n);
p=num;
for(i=0;i*(p+i)=i+1;
i=0;
k=0;
m=0;
while(m{
if(*(p+i)!=0) k++;
if(k==3)
{
*(p+i)=0;
k=0;
m++;
}
i++;
if(i==n) i=0;
}
while(*p==0) p++;
printf("\n%d is left\n",*p);
getch();
}追问这个是指针,不是链表。。这个是指针,不是链表。。
全部回答
  • 1楼网友:一叶十三刺
  • 2021-11-16 19:13
#include
#include
#include
struct Student
{
int num;
struct Student * pNext;
};
struct Student * Creat_List(int *);
void Deal_1(int *, struct Student *);
int main(void)
{
struct Student * pHead;
int cont = 0;
pHead = Creat_List(&cont);
Deal_1(&cont, pHead);
return 0;
}
struct Student * Creat_List(int * p)
{
int i;
bool flag_1 = 1;
struct Student * pHead = (struct Student *)malloc(sizeof(struct Student));
if(NULL == pHead)
{
printf("分配失败,程序结束!");
exit(0);
}
struct Student * pTail = pHead;
pTail->pNext = NULL;
while(flag_1)
{
printf("请输入围成一圈的人数: n = ");
scanf("%d", p);
while(getchar() != '\n')
{
continue;
}
if(*p > 0)
{
flag_1 =0;
}
else
{
printf("您输入的n值有误,请按照提示重新输入!\n");
}
}
for(i=0;i<*p;i++)
{
struct Student * pNew = (struct Student *)malloc(sizeof(struct Student));
if(NULL == pNew)
{
printf("分配失败,程序结束!\n");
exit(0);
}
pNew->num = i+1;
pNew->pNext = NULL;
pTail->pNext = pNew;
pTail = pNew;
}
pTail->pNext = pHead->pNext;
return pHead;
}
void Deal_1(int * p, struct Student * pHead)
{
int i, j, cont = 0;
struct Student * pTail = pHead;
struct Student * q = NULL;
printf("学生离开圈子的顺序是:\n");
for(i=0;;i++)
{
for(j=0;j<3;j++)
{
pTail = pTail->pNext;
if(j == 1)
{
q = pTail;
}
if(j == 2)
{
printf("%-5d", pTail->num);
q->pNext = pTail->pNext;
}
}
cont++;
if(cont == *p-1)
{
printf("\n最后留在圈子中的人原来的序号是:");
}
if(cont == *p)
{
printf("\n");
return;
}
}
return;
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯