标签:blog -- 情况 分析 [] ble turn iostream mat
Q:3对新郎新娘,3个新郎ABC,3个新娘XYZ。A说他将和X结婚,X说她的未婚夫是C,C说他将和Z结婚。已知三人说话都是假的。
思路一:直接用数学分析,然后排除。
#include <iostream> using namespace std; int main() { cout<<"A"<<"--"<<"Z"<<endl; cout<<"B"<<"--"<<"X"<<endl; cout<<"C"<<"--"<<"Y"<<endl; return 0; }
思路二:
husband[0] | wife[i] |
husband[1] | wife[j] |
husband[2] | wife[k] |
规定wife[i]为husband[0]的新娘,wife[j]为husband[1]的新娘,wife[k]为husband[2]的新娘、i,j,k的值在0~2中不断变化,这样随着i,j,k的每一次调整,就能得到一种配对方案。这里必须注意i!=j!=k,否则会出现“2个新郎配一个新娘”的情况。
代码如下:
#include <iostream> using namespace std; int match(int i,int j,int k,char wife[]) { if(wife[i]==‘X‘) return 0; if(wife[k]==‘X‘) return 0; if(wife[k]==‘Z‘) return 0; return 1; } int main() { char husband[3]={‘A‘,‘B‘,‘C‘},wife[3]={‘X‘,‘Y‘,‘Z‘}; int i,j,k; for(i=0;i<3;i++) //新郎A的配对 for(j=0;j<3;j++) //新郎B的配对 for(k=0;k<3;k++) //新郎C的配对 if(i!=j &&j!=k &&i!=k ) { //不能一个新郎配2个新娘或者一个新娘配2个新郎 if(match(i,j,k,wife)) { cout<<"husband wife"<<endl; cout<<"A-------"<<wife[i]<<endl; cout<<"B-------"<<wife[j]<<endl; cout<<"C-------"<<wife[k]<<endl; } } return 0; }
标签:blog -- 情况 分析 [] ble turn iostream mat
原文地址:http://www.cnblogs.com/dd2hm/p/6780742.html