您的位置:首页 > 编程语言 > C语言/C++

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

2015-10-11 10:26 295 查看
<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="" />

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: