【Cpp】分班座位问题,求所有座位方式

<pre name="code" class="cpp">#include <Iostream>
#include <Stdio.h>
#include <vector>
using namespace std;


#define weight 10
#define height 3
#define boyandgirl 30
long double ncount = 0,boysfourgirls=0;
inline void displseat(int seat[height][weight])
{
	for (size_t i = 0; i < height; i++)
	{
		for (size_t i2 = 0; i2 < weight; i2++)
		{
			//cout << seat[i][i2] << " ";
			printf("%d ", seat[i][i2]);
		}

		//cout << endl;
		printf("\n");
	}
	//cout << endl;
	printf("\n");
}
void student(int seat[height][weight],int boys,int girls,int line,int list)
{
	/* 返回 */
	if (boys < 0 || girls < 0)
	{
		return;
	}

	//全部坐满时
	if (boys == 0 && girls == 0)
	{
		//打印出坐满的座位
		//displseat(seat);
		//座位+1
		ncount++;

		//上下左右为女生
		if ((seat[1][0] == 2) && (seat[1][2] == 2) && (seat[0][1] == 2) && (seat[2][1] == 2) && (seat[1][1] == 1))
		{
			boysfourgirls++;
		}
	}
	


	int x, y;

		 
		if (line+1 >= height)//最后一行
		{
			if (list >= weight) //最后一列?
			{
				return;
			}
			/* 最后一行 下一列 */
			x = line;
			y = list + 1;
		}
		/* 不是最后一行 */
		else
		{
			y = (list + 1) % weight;
			if (list == weight - 1)
			{
				x = line + 1;
			}
			else
			{
				x = line;
			}
		}
	
		/* 设置成男生  男生数-1  女生数不变 */
		/* 如果还有男生 */
		if (boys > 0)
		{
			seat[line][list] = 1;
			student(seat, boys - 1, girls, x, y);
		}

		if (girls > 0)
		{
			seat[line][list] = 2;
			student(seat, boys , girls-1, x, y);
		}

}
int main(int argc,char *argv[]) {



	/* 假设2 Boys + 2Girl */
	/*【2*2 座位表		 】*/
	int boys = 1;
	int girls =29;
	int seat[height][weight] = { 0};
	/*
	student(seat, boys, girls, 0, 0);

	cout << boys << "个男生" << girls << "个女生(1代表男生 2代表女生) " << height << "*" << weight << "的座位共有"<<ncount<<"排列方式"<<endl;
	cout << (boysfourgirls/ncount)*100<<"%" << endl;
	*/
	
	for (size_t i = 1; i <= boyandgirl-1; i++)
	{
		boys = i;
		girls = boyandgirl - boys;
		student(seat, boys, girls, 0, 0);
		cout << endl;
		cout << "=============" << endl;
		cout << boys << "个男生" << girls << "个女生(1代表男生 2代表女生) " << height << "*" << weight << "的座位共有" << ncount << "排列方式" << endl;
		cout <<"男生上下左右都是女生的几率:"<< (boysfourgirls / ncount) * 100 << "%" << endl;
		cout << "=============" << endl;
		cout << endl;

		boysfourgirls = 0;
	}
	
	system("Pause");

	return EXIT_SUCCESS;
}


 




<img src="https://img-blog.csdn.net/20151011103203138?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值