n个人围成一个圆桌,按照顺时针的顺序1,2,……n进行编号;某一个人开始报一个数字,然后顺时针的下一个人会报数+1;当某个人报的数字含有7或是7的倍数时,这个人退出游戏,其他人接着报数!直到剩下一个人为止!
》输入
输入n,m,t;n代表人数,m代表开始报数的人的编号;t表示开始报数的人报出的数字是t;
然后接下来有n行,是这n个人的名字!
》输出
输出最后一个人的名字!
》样例输入:
5 3 20
liming
wangze
gongxiangjun
wangming
chenzhen
》样例输出:
Chenzhen
我写的:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node
{
char name[20];
struct node *next;
}node;
typedef struct
{
node *front;
node *rear;
}LQ;
LQ * csh()
{
LQ *p;
p=(LQ *)malloc(sizeof(LQ));
p->front=(node *)malloc(sizeof(node));
p->front->next=NULL;
p->rear=p->front;
return p;
}
LQ * in(LQ *Q,char a[])
{
node *p;
p=(node *)malloc(sizeof(node));
strcpy(p->name,a);
p->next=NULL;
Q->rear->next=p;
Q->rear=p;
return Q;
}
void run(LQ *Q,int n,int m,int t)
{
node *p,*q;
q=Q->front;
p=q->next;
while(--m)
{
q=p;
p=q->next;
}
while(n!=1)
{
if(t%7==0)
{
if(p==Q->rear)
{
q->next=NULL;
Q->rear=q;
q=Q->front;
p=q->next;
}
else
{
q->next=p->next;
p=q->next;
}
n--;
}
else
{
if(p==Q->rear)
{
q=Q->front;
p=q->next;
}
else
{
q=p;
p=q->next;
}
}
t++;
}
printf("%s",p->name);
}
int main()
{
int n,m,t;
char a[20];
LQ *Q;
Q=csh();
scanf("%d %d %d",&n,&m,&t);
int i=n;
while(i--)
{
scanf("%s",a);
in(Q,a);
}
run(Q,n,m,t);
return 0;
}
输出的是第一个名字
请求帮忙看看问题出在哪🤔
#include <bits/stdc++.h>
using namespace std;
struct node
{
int num;
string name;
}p[1001];//用结构体存储人名
queue<node>q;//注意如何定义
bool judge(int x)
{
if(x%7==0)
return 1;//7的倍数
while(x)
{
if(x%10==7)
return 1;
x=x/10;//处理含有7
}
return 0;
}
int main()
{
int n,m,t;
string name;
cin>>n>>m>>t;
for(int i=1;i<=n;i++)
{
cin>>p[i].name;
p[i].num=i;//人名配上顺序
}
for(int i=1;i<=n;i++)
q.push(p[i]);//第一遍入队
for(int i=1;i<=m-1;i++)//从m开始重新入队出队
{
q.push(p[i]);
q.pop();
}
t--;
while(q.size()>1)
{
t++;
node tmp=q.front();
q.pop();
if(!judge(t))//判断数字,同例2
q.push(tmp);
}
printf("%s\n",q.front().name.c_str());//输出
return 0;
}
18行你创建了front,但front是个没有值的节点啊。csh只需要创建头结点就可以了,在in函数中判断front是否为空,再增加新节点
修改后的答案 C语言版
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node
{
char name[20];
struct node *next;
}node;
typedef struct
{
node *front;
node *rear;
}LQ;
LQ * csh()
{
LQ *p;
p=(LQ *)malloc(sizeof(LQ));
p->front=(node *)malloc(sizeof(node));
p->front->next=NULL;
p->rear=p->front;
return p;
}
LQ * in(LQ *Q,char a[])
{
node *p;
p=(node *)malloc(sizeof(node));
strcpy(p->name,a);
p->next=NULL;
Q->rear->next=p;
Q->rear=p;
return Q;
}
int seven(int t)
{
if(t%7==0) return 1;
else
{
while(t!=0)
{
if(t%10==7) return 1;
t/=10;
}
}
return 0;
}
void run(LQ *Q,int n,int m,int t)
{
node *p,*q;
q=Q->front;
p=q->next;
while(--m)
{
q=p;
p=q->next;
}
while(n!=1)
{
if(seven(t))
{
if(p==Q->rear)
{
q->next=NULL;
Q->rear=q;
q=Q->front;
p=q->next;
}
else
{
q->next=p->next;
p=q->next;
}
n--;
}
else
{
if(p==Q->rear)
{
q=Q->front;
p=q->next;
}
else
{
q=p;
p=q->next;
}
}
t++;
}
printf("%s",p->name);
}
int main()
{
int n,m,t;
char a[20];
LQ *Q;
Q=csh();
scanf("%d %d %d",&n,&m,&t);
int i=n;
while(i--)
{
scanf("%s",a);
in(Q,a);
}
run(Q,n,m,t);
return 0;
}