看到过,这个比较明了:http://www.cnblogs.com/boolean/archive/2007/03/23/685776.html

解决方案 »

  1.   

    这是一个《逻辑》题代码需要进行一些a交b,a并b,非a,非b等等的逻辑运算我们单位的笔试题也有类似题,专门用来考核应试者的逻辑思维
      

  2.   

    1-黄-挪威人-dunhill-猫--水
    2-篮-丹麦--blends-马--茶
    3-红-英国--pall mall-鸟--牛奶
    4-绿-德国--prince---鱼---咖啡
    5--白--瑞典--blue master-狗--啤酒
      

  3.   

    /* ---------------------------------------------------------------------------
        判断中用下标表示国家 1英国 2瑞典 3丹麦 4挪威 5德国 (下标0的没使用)
        其他用数组的值表示(房子顺序中值相差1表示邻居)
    数组a    房子顺序  1           2            3        4         5
    数组b    房子颜色  1红色       2绿色        3黄色    4白色     5蓝色
    数组c    香烟      1Pall Mall  2Blue Mater  3Blends  4Dunhill  5Prince
    数组d    饮料      1水         2牛奶        3咖啡    4茶       5啤酒
    数组e    宠物      1鸟         2猫          3马      4鱼       5狗
    -----------------------------------------------------------------------------*/
    #include<iostream>
    using namespace std;
    bool condition(int a[6],int b[6],int c[6],int d[6],int e[6])    {
    /*-----------这些是已经用switch判断了的条件-----------------------------------
    //1、英国人住红色房子
    if(b[1]!=1) return false;
    //2、瑞典人养狗
    if(e[2]!=5) return false;
    //3、丹麦人喝茶
    if(d[3]!=4) return false;
    //9、挪威人住第一间房
    if(a[4]!=1) return false;
    //13、德国人抽Prince香烟
    if(c[5]!=5) return false;
    ------------------------------------------------------------------------------*///8、住在中间房子的人喝牛奶
    {int temp1;
    for(int i=1; i<=5; i++)    { if(a[i]==3) temp1=i;}
        if(d[temp1]!=2) return false;
    }
    //4、绿色房子在白色房子左边
    //5、绿色房子的主人喝咖啡
    //7、黄色房子主人抽Dunhill
    {int temp1,temp2,temp3;
    for(int i=1;i<=5;i++) {
        if(b[i]==2) temp1=i;
        else if(b[i]==4) temp2=i;
            else if(b[i]==3) temp3=i;
        }
        if((a[temp1]+1)!=a[temp2]) return false;
        if(d[temp1]!=3) return false;
        if(c[temp3]!=4) return false;}//14、挪威人住的房子在蓝色房子的隔壁
    {int temp1,temp2;
        temp1=a[4]-1;
        temp2=a[4]+1;
     int temp3=0; int temp4=0;
        for(int j=1;j<=5;j++)    {
        if(a[j]==temp1) temp3=j;
            else if(a[j]==temp2) temp4=j;
        }
        if(temp3==0&&b[temp4]!=5) return false;
        if(temp4==0&&b[temp3]!=5) return false;
        if(temp3!=0&&temp4!=0)
            if(b[temp3]!=5&&b[temp4]!=5) return false;
    }/*-----------------------和抽烟有关的条件--------------------------------------
    //抽Pall Mall香烟的人养鸟
    //抽Blue Master的人喝啤酒
    //抽Blends香烟的人隔壁是养猫的人
    //抽Blends香烟的人邻居有1个是喝水的
    //抽Dunhill香烟的人隔壁是养马的
    ------------------------------------------------------------------------------*/
    {int temp1,temp2,temp3,temp4,temp5,temp;
    for(int i=1; i<=5;i++)    {
        if(c[i]==1) temp1=i;
        else if(c[i]==2) temp2=i;
            else if(c[i]==3) temp3=i;
                else if(c[i]==4) temp=i;
        }
    //6、抽Pall Mall香烟的人养鸟
    //12、抽Blue Master的人喝啤酒
        if(e[temp1]!=1) return false;
        if(d[temp2]!=5) return false;//10、抽Blends香烟的人隔壁是养猫的人
        temp4=a[temp3]-1;
        temp5=a[temp3]+1;
        
    int temp6=0; int temp7=0;
        for(int j=1;j<=5;j++)    {
        if(a[j]==temp4) temp6=j;
            else if(a[j]==temp5) temp7=j;
        }
        if(temp6==0&&e[temp7]!=2) return false;
        if(temp7==0&&e[temp6]!=2) return false;
        if(temp6!=0&&temp7!=0)
            if(e[temp6]!=2&&e[temp7]!=2) return false;
            //15、抽Blends香烟的人邻居有1个是喝水的
    temp6=0; temp7=0;    
    for(int j=1;j<=5;j++)    {
        if(a[j]==temp4) temp6=j;
            else if(a[j]==temp5) temp7=j;
        }
        if(temp6==0&&d[temp7]!=1) return false;
        if(temp7==0&&d[temp6]!=1) return false;
        if(temp6!=0&&temp7!=0)
            if(d[temp6]!=1&&d[temp7]!=1) return false;
            //11、抽Dunhill香烟的人隔壁是养马的
    int temp8,temp9;
        temp8=a[temp]-1;
        temp9=a[temp]+1;
    int temp10=0; int temp11=0;
        for(int j=1;j<=5;j++)    {
        if(a[j]==temp8) temp10=j;
            else if(a[j]==temp9) temp11=j;
        }
        if(temp10==0&&e[temp11]!=3) return false;
        if(temp11==0&&e[temp10]!=3) return false;
        if(temp10!=0&&temp11!=0)
            if(e[temp10]!=3&&e[temp11]!=3) return false;
    }return true;    
    }
    //-----------------同一个类别的数组元素的值必须互不相同-------------------------
    bool compare(int temp[])    {
        if(temp[1]!=temp[2]&&temp[1]!=temp[3]&&temp[1]!=temp[4]&&temp[1]!=temp[5]&&
           temp[2]!=temp[3]&&temp[2]!=temp[4]&&temp[2]!=temp[5]&&
           temp[3]!=temp[4]&&temp[3]!=temp[5]&&
           temp[4]!=temp[5])
        return true;
        else return false;
    }
    //---------------------------打印符合条件的结果-------------------------------
    void print(int a[])    {
        for(int i=1; i<=5; i++)
        cout<<a[i]<<"   "<<a[6+i]<<"   "<<a[12+i]<<"   "<<a[18+i]<<"   "<<a[24+i]<<endl;
        
    }//---------------------------穷举排列------------------------------------------
    void initialization(int temp[], int j)  {
        for(int i1=1; i1<=5; i1++)
            for(int i2=1; i2<=5; i2++)
                for(int i3=1; i3<=5; i3++)
                    for(int i4=1; i4<=5; i4++)
                        for(int i5=1; i5<=5; i5++)  {
                            temp[1]=i1;temp[2]=i2;temp[3]=i3;temp[4]=i4;temp[5]=i5;
                            switch(j)  {
                            case 1: if(temp[4]!=1) continue;break;
                            case 2: if(temp[1]!=1) continue;break;
                            case 3: if(temp[5]!=5) continue;break;
                            case 4: if(temp[3]!=4) continue;break;
                            case 5: if(temp[2]!=5) continue;break;
                            default: break;
                              }
                            if(compare(temp))
                                if(j<5) initialization(temp+6,j+1);
                                    else if(condition(temp-24,temp-18,temp-12,temp-6,temp)) print(temp-24);
                             }      }
    int main()  {
        int a[30];   int j=0;
        initialization(a,j+1);
       }
      

  4.   

    1-黄-挪威人-dunhill-猫--水
    2-篮-丹麦--blends-马--茶
    3-红-英国--pall mall-鸟--牛奶
    4-绿-德国--prince---鱼---咖啡
    5--白--瑞典--blue master-狗--啤酒
      

  5.   

    ..........画图看起来更清楚点....
    http://storage9.myopera.com/btbtd/test/goldfish_by_shawl.qiu.jpg