编程实现舞伴问题 c语言,C语言实现舞伴问题(队列)假设在周末舞会上,男士们(m人)和女士们(n人)进入舞厅时,各自排成一队。跳舞开始时,依次从男...

共回答了12个问题采纳率:91.7%

#includetypedef struct que{

int *qu;

int front,rear;

}Squeue;

void Initqueue(Squeue *q,int len);

void Enqueue(Squeue *q,int n,int len);

int Dequeue(Squeue *q,int len);

int main()

{ int m,n,x,y,i,j,num=0;

Squeue q1,q2;

scanf("%d%d",&m,&n);//Input m and n;

Initqueue(&q1,m);

Initqueue(&q2,n);

for(i=0;iqu=(int*)malloc(len*sizeof(int)) ;

if(!q->qu) exit(0);

q->front=q->rear=0;}

void Enqueue(Squeue *q,int n,int len)

{q->qu[q->rear]=n;

q->rear=(q->rear+1)%len;

}

int Dequeue(Squeue *q,int len)

{int e;

e=q->qu[q->front];

q->front=(q->front+1)%len;

return e;

}在dev和win_tc中通过,在用队列的时候,没有考虑特殊情况,没影响。下面未用队列的算法似乎是很简单,也可以在dev 和win_tc上通过

#include

int main()

{ int m,n;

int i,j,k=0;

int x,y;

printf("Input the number of ladies and gentlemen:");

scanf("%d%d",&m,&n);

printf("Input the location of the man and the woman:");

scanf("%d%d",&x,&y);

if(m==n&&x!=y) printf("No chance!");

else

{for(i=0,j=0;!(i==x&&j==y);i=(i+1)%m,j=(j+1)%n)

{printf("man(%d)woman(%d)n",i+1,j+1); k++;}

printf("nThe number of song is:%dn",k);

}

system("pause");

return 0;

}

1年前

5

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值