当前位置:首页 » 编程语言 » 舞伴C语言

舞伴C语言

发布时间: 2022-07-02 14:14:58

❶ 如何用c语言解决舞伴问题

结构数组dancer中存放跳舞的男女,num是跳舞的人数。//男士队列初始化//女士队列初始化//依次将跳舞者依其性别入队//依次输入男女舞伴名//输出女士剩余人数及队头女士的名字//输出男队剩余人数及队头者名字

#include<stdio.h>//舞伴配对问题(用循环队列实现)//
#define queuesize 100
typedef struct //定义结构体-人员信息//
{
char name[20];
char sex;
} datatype;
typedef struct //定义结构体-循环队列//
{
int front;
int rear;
int count;
datatype dancers[queuesize];
} cirqueue;
void initqueue(cirqueue *q)//建立空队列//
{
q->front=q->rear=NULL;
q->count=0;
}
int queueempty(cirqueue *q)//判断队列是否为空,为空返回1,否则返回0//
{
return q->count==NULL;
}
void enqueue(cirqueue *q,datatype p)//进队函数,使p进入队列//
{
q->count++;
q->dancers[q->rear]=p;
q->rear=(q->rear+1)%queuesize;
}
datatype dequeue(cirqueue *q)//出队函数,用p返回出队值//
{
datatype p;
while(!queueempty(q))
{
p=q->dancers[q->front];
q->count--;
q->front=(q->front++)%queuesize;
return p;
}
}
datatype queuefront(cirqueue *q)//访问队头元素//
{
return q->dancers[q->front];
}
void dancepartners(datatype dancers[],int num)//舞伴配对函数//
{
int i;
datatype p;
cirqueue maledancer,femaledancer;//定义两个循环队列结构体//
initqueue(&maledancer);//调用initqueue()函数置空队列maledancer//
initqueue(&femaledancer);//调用initqueue()函数置空队列femaledancer//
for(i=0;i<num;i++)
{
p=dancers[i];//结构体整体赋值//
if(p.sex=='f')//女的进队列femaledancer//
{
enqueue(&femaledancer,p);
printf("%s进女队\n",p.name);
}
else//男的进队列maledancer//
{
enqueue(&maledancer,p);
printf("%s进男队\n",p.name);
}
}
printf("配对情况如下:\n");
while (!queueempty(&femaledancer)&&!queueempty(&maledancer))//当两个队列都不为空时,出队配对//
{
p=dequeue(&maledancer);
printf("男:%s",p.name);
p=dequeue(&femaledancer);
printf("和女:%s配对成功\n",p.name);
}
if(!queueempty(&femaledancer))//女队不为空,输出女队第一个等待人姓名//
{
printf("女队中还有%d个人在等待!\n",femaledancer.count);
p=queuefront(&femaledancer);
printf("女队中第一个等待的是:%s\n",p.name);
}
else if(!queueempty(&maledancer))//男队不为空,输出男队第一个等待人姓名//
{
printf("男队中还有%d个人在等待!\n",maledancer.count);
p=queuefront(&maledancer);
printf("男队中第一个等待的是:%s\n",p.name);
}
else printf("^-^没有人剩余^-^!\n");
}
main()
{
datatype p,dancers[queuesize];
int i,num;
printf("输入男女dancer的总人数:\n");
scanf("%d",&num);
printf("输入姓名和性别,如: \nzhangsan\nm\n");
for(i=0;i<num;i++)
{
scanf("%s",&dancers[i].name);
getchar();
scanf("%c",&dancers[i].sex);
if(dancers[i].sex!='f'&&dancers[i].sex!='m')//判断输入信息是否合法//
printf("第%d个人的信息有误,请重新输入",i--);
}
dancepartners(&dancers[0],n);
getchar();
}

❷ 利用循环队列解决舞伴配对问题, C语言版

#include<stdio.h>int main()
{
int boy,girl,mun;
printf("请依次输入男,女以及轮数:");
scanf("%d%d%d",&boy,&girl,&mun);
for(int Nboy=0,Ngirl=0,Nmun=0;Nmun<=mun;++Nmun)
{
if (Nboy>=boy)
{
Nboy = 0;
}
else
{
++Nboy;
}
if (Ngirl>=girl)
{
Ngirl = 0;
}
else
{
++Ngirl;
}
printf("第%d轮\t男%d号----女%d号\n",Nmun,Nboy,Ngirl);
}
return 0;
}你看看是这样不、?/这是运行结果,你看看是不是??有什么问题在提问

❸ 求数据结构C++舞伴配对问题程序代码

intmain()
{
list<dancer>Mdancer;//定义dancer类链表,保存男舞者信息
list<dancer>Fdancer;//定义dancer类链表,保存女舞者信息

while(m)//输入舞者信息
{
dancerperson;//定义临时dancer变量,保存输入信息
person.input();
if(person.sex!="男"&&person.sex!="女")//判断输入性别是否符合要求
{
cout<<"输入的舞者性别不正确,请重新输入";
continue;
}
elseif(person.hour<0||person.hour>23||person.minute<0||person.minute>59||person.second<0||person.second>59)
{
cout<<"输入的舞者到场时间不正确,请重新输入";
continue;
}
else
{
if(person.sex=="男")
Mdancer.push_back(person);//将男舞者信息存入男性链表
else
Fdancer.push_back(person);//将女舞者信息存入女性链表
}
cout<<" 是否继续录入舞者信息:1为继续录入,0为不再录入! ";
cin>>m;
}
cout<<"首先为男性舞者排队! ";
pai_i(Mdancer);//对男舞者进行排队
cout<<"然后为女性舞者排队! ";
pai_i(Fdancer);//对女舞者进行排队

/*cout<<" 排队后:"<<endl;//输出排队后所有男舞者信息
for(p=Mdancer.begin();p!=Mdancer.end();p++)
cout<<(*p).name<<""<<(*p).sex<<""<<(*p).height<<""<<(*p).hour
<<":"<<(*p).minute<<":"<<(*p).second<<endl;*/

cout<<"请输入跳舞的最大轮数:";
intk=0;//跳舞轮数
cin>>k;
for(inti=0;i<k;i++)/*舞伴配对*/
{
intnum1=Mdancer.size();//男舞者个数
intnum2=Fdancer.size();//女舞者个数
intmin=num1<num2?num1:num2;
cout<<"第"<<i+1<<"轮舞伴配对情况: ";
if(min<=N)//舞者对数小于等于舞场容纳量,则全部舞者可进入
{
for(inti=0;i<min;i++)//输出第k轮的舞伴
{
cout<<Mdancer.front().name<<"<-->"<<Fdancer.front().name<<endl;
Mdancer.push_back(Mdancer.front());//将配对的舞者重新排在队尾
Fdancer.push_back(Fdancer.front());
Mdancer.pop_front();//将配对的舞者从队首删除
Fdancer.pop_front();
}
}
else//舞者对数大于舞场容纳量
{
for(inti=0;i<N;i++)//输出第k轮的舞伴
{
cout<<Mdancer.front().name<<"<-->"<<Fdancer.front().name<<endl;
Mdancer.push_back(Mdancer.front());//将配对的舞者重新排在队尾
Fdancer.push_back(Fdancer.front());
Mdancer.pop_front();//将配对的舞者从队首删除
Fdancer.pop_front();
}
}
/*每跳完一轮询问是否有舞者要离开*/
cout<<" 是否有舞者要离开,有舞者离开请输入1,否则输入0! ";
intlev=0;//有舞者离开的标志,1为有舞者要离开
cin>>lev;
/*跳完舞的人默认排到了队伍中,若选择离开(还没跳舞的人也可以选择离开)*/
if(lev==1)
{
cout<<"请输入要离开的舞者姓名:";
cin>>leaver.name;
cout<<"请输入要离开的舞者性别:";
cin>>leaver.sex;
if(leaver.sex=="男")
Mdancer.remove_if(leave);//根据姓名从队列中删除舞者
elseif(leaver.sex=="女")
Fdancer.remove_if(leave);
cout<<leaver.name<<"已离开舞场! ";
}
}
return0;
}

完整文件在附件,你看下是否符合要求,有问题再问!

❹ 谁能告诉我如何用c语言编写数据结构中的舞伴问题

typedef struct{
char name[20];
char sex;
}Person;
typedef Person DataType;

void DancePartners(Person dancer[],int num)
{
int i;
Person p;
CirQueue Mdancers,Fdancers;
InitQueue(&Mdancers);
InirQueue(&Fdancers);
for(i=0;i<num;i++){
p=dancer[i];
if(p.sex=='F')
EnQueue(&Fdancers,p);
else
EnQueue(&Mdancers,p);
}
printf("The dancing partners are:\n\n");
while(!QueueEmpty(&Fdancers)&&!QueueEmpty (&Mdancers)){
p=DeQueue(&Fdancers);
printf("%s ",p.name);
p=DeQueue(&Mdancers);
printf("%s\n",p.name);
}
if(!QueueEmpty(&Fdancers)){
printf("\nThere are %d women waiting for the next round.\n",Fdancers.count);
p=QueueFront(&Fdancers);
printf("%s will be the first to get a partner.\n",p.name);
}else
if(!QueueEmpty(&Mdancers)){
printf("\nThere are %d men waiting for the next round.\n",Mdancers.count);
p=QueueFront(&Mdancers);
printf("%s will be the first to get a partner.\n",p.name);
}
}

❺ C(C++也行)语言题(舞伴选择)求代码

#include<cstdio>
#include<algorithm>
#definerep(i,l,r)for(inti=l;i<=r;++i)
#defineper(i,r,l)for(inti=r;i>=l;--i)
#ifdefWIN32
#defineLD"%I64d "
#else
#defineLD"%lld "
#endif
typedeflonglongLL;
LLdfs(intl,intr){
if(l>=r)return0;
if(r-l==1)return(LL)l*r;
intmid=(l+r)>>1;
returndfs(l,mid)+dfs(mid+1,r);
}
intmain(){
intt,n;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
printf(LD,dfs(1,n));
}
}

O(t*nlogn)

❻ C语言实现舞伴问题(队列)

#include<stdio.h>typedef 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;i<m;i++) q1.qu[i]=i+1;
for(i=0;i<n;i++) q2.qu[i]=i+1; scanf("%d%d",&x,&y);//Input x and y
if(x!=y&&m==n) { printf("No chance");sleep(1000);exit(0);}
i=Dequeue(&q1,m);
j=Dequeue(&q2,n);
printf("M<-->W\n");
while(!(x==i&&y==j))
{Enqueue(&q1,i,m);<br> Enqueue(&q2,j,n);<br> printf("%d<-->%d\n",i,j);<br> i=Dequeue(&q1,m);<br> j=Dequeue(&q2,n);<br> num++;<br> }
printf("%d<-->%d\n",i,j);
printf("The number is %d\n",num+1);
free(q1.qu);
free(q2.qu);
system("pause");
return 0;
}
void Initqueue(Squeue *q,int len)
{
q->qu=(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;<br> q->rear=(q->rear+1)%len;<br>}
int Dequeue(Squeue *q,int len)
{int e;<br> e=q->qu[q->front];<br> q->front=(q->front+1)%len;<br> return e;<br>}在dev和win_tc中通过,在用队列的时候,没有考虑特殊情况,没影响。下面未用队列的算法似乎是很简单,也可以在dev 和win_tc上通过
#include<stdio.h>
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)<br> {printf("man(%d)<-->woman(%d)\n",i+1,j+1); k++;}
printf("\nThe number of song is:%d\n",k);
}
system("pause");
return 0;
}

❼ C语言舞伴问题--循环队列

调用 Initqueue的时候,参数还没初始化好
Initqueue(&Mdancers);
Initqueue(&Fdancers);

❽ 用c语言编写舞伴配对程序,急求,快的可以加分

程序写了一个,你看看是不是能满足你要求,满意请采纳!!

下面是程序运行的结果:

热点内容
二级程序编译答案 发布:2024-05-03 18:41:35 浏览:653
领动自动精英版是哪个配置 发布:2024-05-03 18:37:30 浏览:150
java编译器中cd什么意思 发布:2024-05-03 18:36:00 浏览:389
传奇服务器如何刷钱 发布:2024-05-03 18:36:00 浏览:977
安卓版twitter怎么注册 发布:2024-05-03 18:28:05 浏览:893
Python逻辑优先级 发布:2024-05-03 18:26:14 浏览:267
linux查看svn密码 发布:2024-05-03 18:12:47 浏览:804
地铁逃生怎么进入游戏安卓 发布:2024-05-03 17:49:35 浏览:992
aws云存储 发布:2024-05-03 17:48:50 浏览:955
安卓微信王者号怎么转成苹果 发布:2024-05-03 17:44:38 浏览:745