【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="" />
相关文章推荐
- ACM题目中输入数据的处理(C++版)
- C语言之转义字符、变量的内存机制
- 【LeetCode从零单刷】Ugly Number I, II & Super Ugly Number
- 【C语言】判断花括号{}是否匹配
- C++结构体内存对齐小结
- VS2010中“工具>选项中的VC++目录编辑功能已被否决”解决方法
- C++与类型转换相关的四个关键字及其特点
- [C/C++基础知识] 一篇就让你彻底搞懂qsort快速排序的文章
- Leetcode NO.125 Valid Palindrome
- C语言的标识符命名规则
- 熊猫阿波的故事及C++保留小数点的问题
- C++primer学习:关联容器练习(4)
- C++STL之string上
- 转载:读懂C/C++递归
- hdoj 2030 汉字统计 (汉字机内码特点) C++
- C语言的随机数
- C语言 用链表实现电话本的功能
- C语言 用顺序表实现电话本的功能
- C++ 中stringstream 的作用
- 带你玩转Visual Studio——带你理解微软的预编译头技术