c语言如何将男女生分开住宿

c语言如何将男女生分开住宿,男生住一起,女生住一起并且用什么字符来实现

  • 这篇博客: C语言队列解决舞伴匹配问题中的 舞伴问题:男女站成两队依次结成舞伴,两队人数不同,人数多的队伍则会出现无人匹配的情况,所以多出的人等待下一轮其他组完成跳舞,再继续结成舞伴。 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • #include <stdio.h>
    #include <stdlib.h> 
    #include <string.h>
    #include<time.h>
    #define MAXSIZE 13
    typedef struct
    {
    	char name[20];
    	char sex;
    }Person;
    typedef Person DataType;
    typedef struct
    {	
    	DataType data[MAXSIZE];
    	int front,rear;
    }SqQueue;
    
    
    //初始化队列 
    void initQueue(SqQueue &Q)
    {
    	Q.front = Q.rear = -1;
    }
    
    //判断队空 
    int queueEmpty(SqQueue Q)
    {
    	return (Q.front == Q.rear ? 1:0); 
    }
    
    //判断队满
    int queueFull(SqQueue Q)
    {
    	return ((Q.rear + 1)%MAXSIZE == Q.front? 1:0); 
    }
     
    
    //进队
    int enQueue(SqQueue &Q,DataType e)
    {
    	//队满 
    	if(queueFull(Q))
    	{
    		return 0;	
    	}
    	//rear加1,队尾位置移动 
    	Q.rear = (Q.rear + 1)%MAXSIZE;
    	Q.data[Q.rear] = e;
    	return 1;
    }
    
    //出队
    int deQueue(SqQueue &Q,DataType &e)
    {
    	//队为空 
    	if(queueEmpty(Q))
    	{
    		return 0;	
    	}
    	//front加1,队头位置上移 
    	Q.front = (Q.front + 1)%MAXSIZE; 
    	e = Q.data[Q.front];
    	return 1;
    } 
    //取队头
    int queueFront(SqQueue &Q,DataType &e)
    {
    	if(queueEmpty(Q))
    	{
    		return 0;
    	}
    	e = Q.data[(Q.front + 1)%MAXSIZE];
    	return 1;	
    } 
    
    //求队列的长度
    int queueLength(SqQueue Q)
    {
    	return (Q.rear-Q.front+MAXSIZE)%MAXSIZE;	
    } 
    
    void dancePartner(Person dancer[],int num)
    {
    	Person p;
    	SqQueue Mdancers,Fdancers;
    	initQueue(Mdancers);
    	initQueue(Fdancers);
    	for(int i = 0;i<num;i++)
    	{
    		//依次按姓名入队 
    		p = dancer[i];
    		if(p.sex == 'F')
    		{
    			//排入女队 
    			enQueue(Fdancers,p);
    		}else
    		{	
    			//排入男队 
    			enQueue(Mdancers,p);
    		}
    		putchar(10);
    		printf("================================\n");
    		printf("The dancing partners are: \n \n");
    		while(!queueEmpty(Fdancers) && !queueEmpty(Mdancers))
    		{
    			deQueue(Fdancers,p);
    			printf("%s	",p.name);
    			deQueue(Mdancers,p);
    			printf("%s\n",p.name);
    		}
    		if(!queueEmpty(Fdancers))
    		{
    			printf("\nThere are %d women wating for the next round.\n",queueLength(Fdancers)); 
    			queueFront(Fdancers,p);
    			printf("%s will be the first to get a partner.\n",p.name); 
    		}
    		
    		if(!queueEmpty(Mdancers))
    		{
    
    			printf("\nThere are %d men wating for the next round.\n",queueLength(Mdancers)); 
    			queueFront(Mdancers,p);
    			printf("%s will be the first to get a partner.\n",p.name); 
    		}
    	}
    }
    
    int main()
    {
    	srand((unsigned)time(NULL));
    	Person pArr[100];
    	//动态生成数据测试 
    	for(int i= 0;i<MAXSIZE;i++)
    	{
    		int sexNum = rand()%2;
    		char pre[10] = "";
    		strcpy(pre,rand()%2 == 0 ? "男":"女");
    		strcmp(pre,"男") == 0 ? pArr[i].sex='M' :pArr[i].sex='F';
    		char suf[10] = "";
    		sprintf(suf,"%d",rand()%10000);
    		strcpy(pArr[i].name,strcat(pre,suf));
    	}
    	printf("生成男女生数据为:\n");
    	for(int i= 0;i<MAXSIZE;i++){
    		printf("%s=%c   ",pArr[i].name,pArr[i].sex);
    	}
    	dancePartner(pArr,10);
    	return 0;
    }

  • 以下回答由chatgpt基于相关博客总结生成:

    根据参考资料,我们可以使用C语言编写一个程序来实现将男生和女生分开住宿的功能。以下是具体的解决方案:

    1. 创建一个循环队列的结构体Queue,用于存储男生和女生的队列信息。
    struct Queue {
        int Front;
        int Rear;
        char elem[100][100];
        int Queuesize;
    };
    
    1. 定义创建队列的函数Creat_Queue,用于输入跳舞人数和各跳舞人名。
    void Creat_Queue(Queue &Q) {
        int n, i;
        Q.Front = Q.Rear = 0;
        while(1) {
            printf("请输入跳舞人数:");
            scanf("%d", &n);
            if(n < 100)
                break;
            else
                printf("输入人数错误,请重新输入");
        }
        Q.Queuesize = n + 1; 
        printf("请输入各跳舞人名:");
        for(i = 0; i < n; i++)
            scanf("%s", Q.elem[i]);
        Q.Rear = n; 
    }
    
    1. 定义判断队列是否为空的函数QueueEmpty,用于判断队列是否为空。
    int QueueEmpty(Queue Q) {
        if(Q.Front == Q.Rear)
            return 1;
        else
            return 0;
    }
    
    1. 定义入队函数PutQueue,用于将舞伴放入队列中。
    void PutQueue(Queue &Q, char *str) {
        int l = strlen(str);
        for(int i = 0; i <= l; i++) {
            Q.elem[Q.Rear][i] = str[i];
        }
        Q.Rear = (Q.Rear + 1) % Q.Queuesize;
    }
    
    1. 定义比较两个字符串是否相同的函数is_Same,用于判断两个字符串是否相同。
    bool is_Same(char s1[], char s2[]) {
        int l1 = strlen(s1), l2 = strlen(s2);
        if(l1 != l2)
            return false;
        for(int i = 0; i <= l1; i++) {
            if(s1[i] != s2[i])
                return false;
        }
        return true;
    }
    
    1. 定义取较小值的函数min,用于取两个值中的较小值。
    int min(int a, int b) {
        if(a < b)
            return a;
        return b;
    }
    
    1. 定义判断舞伴配对的函数Judge_Queue,用于计算舞曲的轮数。
    int Judge_Queue(Queue &M, Queue &W) {
        int k1 = 0, k, m = 0;
        char man[100], wuman[100];
        printf("请输入需要配对的人:");
        printf("男:");
        scanf("%s", man);
        printf("女:");
        scanf("%s", wuman);
        printf("您希望他们这是第几次互相跳舞");
        scanf("%d", &k);
        while(k1 < k) {
            int m_size = M.Queuesize - 1;
            int w_size = W.Queuesize - 1;
            int dance_size = min(m_size, w_size);
            for(int i = 1; i <= dance_size; i++) {
                if(is_Same(man, M.elem[M.Front]) && is_Same(wuman, W.elem[W.Front])) {
                    k1 = k1 + 1;
                }
                PutQueue(M, M.elem[M.Front]);
                PutQueue(W, W.elem[W.Front]);
                M.Front = (M.Front + 1) % M.Queuesize;
                W.Front = (W.Front + 1) % W.Queuesize;
            }
            m = m + 1;
            if((M.Queuesize + 1) * (W.Queuesize + 1) < m && k1 == 0) {
                printf("错误,这两个人永远不能配对");
                return 0;
            }
        }
        return m;
    }
    
    1. 在main函数中,调用以上函数实现舞伴配对的功能。
    int main() {
        Queue M, W;
        printf("男队:\n");
        Creat_Queue(M);
        printf("女队:\n");
        Creat_Queue(W);
        printf("发生在第%d轮舞曲", Judge_Queue(M, W));
        return 0;
    }
    

    根据以上程序,你可以通过输入男生和女生的队列信息来获取他们配对的舞曲轮数。在程序中使用了字符数组来存储队列的信息,并通过循环队列的方式实现队列的操作。

这个要看你的代码,你是希望将一个数组或者列表拆分成两个,一个全部是男的,一个全部是女的么?