c++设计楼层分配问题!!!急求!!!

来源:百度知道 编辑:UC知道 时间:2024/07/02 07:10:57
2. 问题详细描述
特定条件:现有五层的公寓分配给A、B、C、D、E五个人住。其中A不住1楼,E不住5楼,C和D住上下楼(可以C比D住的高,也可以D比C住的高),E比C住的高。
谢谢!!!!!

#include <iostream.h>
#include <fstream.h>
#include <math.h>

int pos[5];//pos数组0..4分别代表a..e五个人,每个元素的值代表每个人住的楼层。

void print (int pos[5])//输出函数
{
for (int i=0;i<=4;i++)
cout<<char('A'+i)<<"住"<<pos[i]<<"楼 ";
cout<<"\n";
}

bool filter (int pos[5])//对穷举的到的数组进行过滤,但凡数组中有两个以上元素相等的都返回false
{
int temp;
for (int i=0;i<=4;i++)//比较数组元素,有两个相等的都返回false,不符合题目条件
{temp=pos[i];
for (int j=i+1;j<=4;j++)
if(temp==pos[j])
return false;
}

return true;
}
bool judge (int pos[5])
{//根据其中A不住1楼,B不住5楼...等条件判断,pos[0]是A住的楼层,pos[0]是B住的楼层...不符合的返回返回false

if(( pos[0]==1)||(pos[1]==5)||(abs(pos[2]-pos[3])!=1))
return false;
if(pos[4]<pos[2])
return false;

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