📄 40.c
字号:
#include<stdio.h>
void gramescount() //游戏规则
{
int i,k,m,n,num[30],N,*p;
printf("n=");
scanf("%d",&n);
printf("\nN=");
scanf("%d",&N);
p=num;
for(i=0;i<n;i++)
*(p+i)=i+1;
i=0; //i为每次循环时的计数变量
k=0; //k为按1,2,3报数时的计数变量
m=0; //m为退出人数
while(m<n-1)
{
{
if(*(p+i)!=0) k++;
if(k==N)
{
*(p+i)=0;
k=0;
m++;
}
i++;
if(i==n) i=0;
}
}
while(*p==0) p++;
printf("The last number is NO.%d\n",*p);
}
/*
#include <stdio.h>
#define N 10//人数
#define M 3//报多少淘汰
void main()
{
int a[N];
int *p;
int i;
int n;
for(i=0;i<N;i++)
{
a[i]=i+1;//初始化人的序号
}
n=N;//游戏剩余人数
p=a;//指针指向数组首址
while(n>1)//当剩余人数大于1
{
for(i=1;i<=M;i++)//
{
while(*p==0)//如果人已被淘汰,直接找下个人
{
p++;
if(p==a+N)//当超出最后一个人时,从头开始
p=a;
}
if(i==M)//当人不是被淘汰的,且报到被淘汰的数字时
{
printf("淘汰的人的序号为:%d\n",*p);//打印此人的号码
*p=0;//淘汰此人
n--;//游戏人数减1
}
p++;
if(p==a+N)//当超出最后一个人时,从头开始
p=a;
}
}
for(i=0;i<N;i++)//输出最后剩余的人
{
if(a[i]!=0)
printf("第%d个人留下!\n",a[i]);
}
}
//链表做1-3报数淘汰人
#include <stdio.h>
#include <stdlib.h>
#define NULL 0
#define LEN sizeof(struct people)
int n=0;
struct people
{
int num;
struct people *next;
};
struct people *creat(int i)
{
struct people *head,*p1,*p2;
head=NULL;
p1=(struct people *)malloc(LEN);
p1->num=n+1;
while(n<i)
{
n++;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct people *)malloc(LEN);
p1->num=n+1;
}
p2->next=head;
return head;
}
struct people *last(struct people *head)
{
int i=1;
struct people *p1,*p2;
p1=head;
for(;n>1;i++)
{
if((i+1)%3==0)
{
p2=p1;
}
if(i%3==0)
{
if(n>2)
{
p2->next=p1->next;
}
else if(n==2)
{
p2->next=NULL;
}
free(p1);
n--;
if(n>1)
p1=p2->next;
}
if(i%3!=0)
p1=p1->next;
}
head=p2;
return head;
}
void print(struct people *head)
{
struct people *p;
p=head;
while(p!=NULL)
{
printf("%d为最后留下的人!\n",p->num);
p=p->next;
}
}
void main()
{
struct people *head;
int i;
while(1)
{
puts("请输入游戏人数!");
scanf("%d",&i);
if(i>=2)
break;
else
printf("少于2个人无法玩此游戏啊!\n");
}
n=0;
head=creat(i);
head=last(head);
print(head);
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -