不能一会儿用口算出来,直接在网上搜得了下面资料
1 2 3 4 5
挪威 丹麦 英国 德国 瑞典
黄色 蓝色 红色 绿色 白色
Dunhill Blend pall mall prinee bine msier
开水 茶 牛奶 咖啡 啤酒
猫 马 鸟 鱼 狗
#include <iostream.h>
char* people[5] = {NULL};
char* color[5] = {NULL};
char* smoke[5] = {NULL};
char* drink[5] = {NULL};
char* pet[5] = {NULL};
/*
char* people[] = {"挪威","丹麦","英国","德国","瑞典"};
char* color[] = {"黄色","蓝色","红色","绿色","白色"};
char* smoke[] = {"Dunhill","Blend","Pall Mall","Prinee","Bine Masier"};
char* drink[] = {"开水","茶","牛奶","咖啡","啤酒"};
char* pet[] = {"猫","马","鸟","鱼","狗"};
*/
void print_single_result(const char* remark, char* a[])
{
cout << remark << endl;
for( int i = 0; i < 5; i ++ )
{
if( NULL == a[i] )
cout << "NULL";
else
cout << a[i];
cout << "\t" << flush;
}
cout << endl;
}
void print_result()
{
print_single_result("people", people);
print_single_result("color", color);
print_single_result("smoke", smoke);
print_single_result("drink", drink);
print_single_result("pet", pet);
}
int foo(const int level);
int main()
{
if( 0 == foo(0) )
{
print_result();
}
return 0;
}
Top
{
/*
0. (drink[2]=="牛奶")
1. (people[0]=="挪威")
2. (people[i]=="英国"&&color[i]=="红色")
3. (people[i]=="瑞典"&&pet[i]=="狗")
4. (people[i]=="丹麦"&&drink[i]=="茶")
5. (color[i]=="绿色"&&drink[i]=="咖啡")
6. (smoke[i]=="Bine Masier"&&drink[i]=="啤酒")
7. (smoke[i]=="Pall Mall"&&pet[i]=="鸟")
8. (color[i]=="黄色"&&smoke[i]=="Dunhill")
9. (people[i]=="德国"&&smoke[i]=="Prinee")
10. (color[i]=="绿色"&&color[j]=="白色"&&(i<j))
11. (smoke[i]=="Dunhill"&&pet[j]=="马"&&(i=j-1||i=j+1))
12. (smoke[i]=="Blend"&&pet[j]=="猫"&&(i=j-1||i=j+1))
13. (people[i]=="挪威"&&color[j]=="蓝色"&&(i=j-1||i=j+1))
14. (drink[i]=="开水"&&smoke[j]=="Blend"&&(i=j-1||i=j+1))
*/
switch(level)
{
case 0 :
{
int isNull = 0;
if( drink[2] != NULL && strcmp(drink[2],"牛奶") ) return 1;
if( drink[2] == NULL ) isNull = 1;
if( isNull ) drink[2] = "牛奶";
if( 0 == foo(level+1) ) return 0;
if( isNull ) drink[2] = NULL;
return 1;
}
break;
case 1 :
{
int isNull = 0;
if( people[0] != NULL && strcmp(people[0],"挪威") ) return 1;
if( people[0] == NULL ) isNull = 1;
if( isNull ) people[0] = "挪威";
if( 0 == foo(level+1) ) return 0;
if( isNull ) people[0] = NULL;
return 1;
}
break;
case 2 :
{
int i = 0;
for( i = 0; i < 5; i ++ )
{
int isNull1=0, isNull2=0;
if( people[i] != NULL && strcmp(people[i],"英国") ) continue;
if( color[i] != NULL && strcmp(color[i],"红色") ) continue;
if( people[i] == NULL ) isNull1 = 1;
if( color[i] == NULL ) isNull2 = 1;
if( isNull1 ) people[i] = "英国";
if( isNull2 ) color[i] = "红色";
if( 0 == foo(level+1) ) return 0;
if( isNull1 ) people[i] = NULL;
if( isNull2 ) color[i] = NULL;
}
return 1;
}
break;
case 3 :
{
int i = 0;
for( i = 0; i < 5; i ++ )
{
int isNull1=0, isNull2=0;
if( people[i] != NULL && strcmp(people[i],"瑞典") ) continue;
if( pet[i] != NULL && strcmp(pet[i],"狗") ) continue;
if( people[i] == NULL ) isNull1 = 1;
if( pet[i] == NULL ) isNull2 = 1;
if( isNull1 ) people[i] = "瑞典";
if( isNull2 ) pet[i] = "狗";
if( 0 == foo(level+1) ) return 0;
if( isNull1 ) people[i] = NULL;
if( isNull2 ) pet[i] = NULL;
}
return 1;
}
break;
case 4 :
{
int i = 0;
for( i = 0; i < 5; i ++ )
{
int isNull1=0, isNull2=0;
if( people[i] != NULL && strcmp(people[i],"丹麦") ) continue;
if( drink[i] != NULL && strcmp(drink[i],"茶") ) continue;
if( people[i] == NULL ) isNull1 = 1;
if( drink[i] == NULL ) isNull2 = 1;
if( isNull1 ) people[i] = "丹麦";
if( isNull2 ) drink[i] = "茶";
if( 0 == foo(level+1) ) return 0;
if( isNull1 ) people[i] = NULL;
if( isNull2 ) drink[i] = NULL;
}
return 1;
}
break;
Top
{
int i = 0;
for( i = 0; i < 5; i ++ )
{
int isNull1=0, isNull2=0;
if( color[i] != NULL && strcmp(color[i],"绿色") ) continue;
if( drink[i] != NULL && strcmp(drink[i],"咖啡") ) continue;
if( color[i] == NULL ) isNull1 = 1;
if( drink[i] == NULL ) isNull2 = 1;
if( isNull1 ) color[i] = "绿色";
if( isNull2 ) drink[i] = "咖啡";
if( 0 == foo(level+1) ) return 0;
if( isNull1 ) color[i] = NULL;
if( isNull2 ) drink[i] = NULL;
}
return 1;
}
break;
case 6 :
{
int i = 0;
for( i = 0; i < 5; i ++ )
{
int isNull1=0, isNull2=0;
if( smoke[i] != NULL && strcmp(smoke[i],"Bine Masier") ) continue;
if( drink[i] != NULL && strcmp(drink[i],"啤酒") ) continue;
if( smoke[i] == NULL ) isNull1 = 1;
if( drink[i] == NULL ) isNull2 = 1;
if( isNull1 ) smoke[i] = "Bine Masier";
if( isNull2 ) drink[i] = "啤酒";
if( 0 == foo(level+1) ) return 0;
if( isNull1 ) smoke[i] = NULL;
if( isNull2 ) drink[i] = NULL;
}
return 1;
}
break;
case 7 :
{
int i = 0;
for( i = 0; i < 5; i ++ )
{
int isNull1=0, isNull2=0;
if( smoke[i] != NULL && strcmp(smoke[i],"Pall Mall") ) continue;
if( pet[i] != NULL && strcmp(pet[i],"鸟") ) continue;
if( smoke[i] == NULL ) isNull1 = 1;
if( pet[i] == NULL ) isNull2 = 1;
if( isNull1 ) smoke[i] = "Pall Mall";
if( isNull2 ) pet[i] = "鸟";
if( 0 == foo(level+1) ) return 0;
if( isNull1 ) smoke[i] = NULL;
if( isNull2 ) pet[i] = NULL;
}
return 1;
}
break;
case 8 :
{
int i = 0;
for( i = 0; i < 5; i ++ )
{
int isNull1=0, isNull2=0;
if( color[i] != NULL && strcmp(color[i],"黄色") ) continue;
if( smoke[i] != NULL && strcmp(smoke[i],"Dunhill") ) continue;
if( color[i] == NULL ) isNull1 = 1;
if( smoke[i] == NULL ) isNull2 = 1;
if( isNull1 ) color[i] = "黄色";
if( isNull2 ) smoke[i] = "Dunhill";
if( 0 == foo(level+1) ) return 0;
if( isNull1 ) color[i] = NULL;
if( isNull2 ) smoke[i] = NULL;
}
return 1;
}
break;
case 9 :
{
int i = 0;
for( i = 0; i < 5; i ++ )
{
int isNull1=0, isNull2=0;
if( people[i] != NULL && strcmp(people[i],"德国") ) continue;
if( smoke[i] != NULL && strcmp(smoke[i],"Prinee") ) continue;
if( people[i] == NULL ) isNull1 = 1;
if( smoke[i] == NULL ) isNull2 = 1;
if( isNull1 ) people[i] = "德国";
if( isNull2 ) smoke[i] = "Prinee";
if( 0 == foo(level+1) ) return 0;
if( isNull1 ) people[i] = NULL;
if( isNull2 ) smoke[i] = NULL;
}
return 1;
}
break;
Top
{
int i = 0;
for( i = 0; i < 5; i ++ )
{
int isNull1=0;
if( color[i] != NULL && strcmp(color[i],"绿色") ) continue;
if( color[i] == NULL ) isNull1 = 1;
if( isNull1 ) color[i] = "绿色";
int j;
for( j = i+1; j < 5; j ++ )
{
int isNull2=0;
if( color[j] != NULL && strcmp(color[j],"白色") ) continue;
if( color[j] == NULL ) isNull2 = 1;
if( isNull2 ) color[j] = "白色";
if( 0 == foo(level+1) ) return 0;
if( isNull2 ) color[j] = NULL;
}
if( isNull1 ) color[i] = NULL;
}
return 1;
}
break;
case 11 :
{
int i = 0;
for( i = 0; i < 5; i ++ )
{
int isNull1=0;
if( smoke[i] != NULL && strcmp(smoke[i],"Dunhill") ) continue;
if( smoke[i] == NULL ) isNull1 = 1;
if( isNull1 ) smoke[i] = "Dunhill";
if( i-1>=0 )
{
int j = i-1;
int isNull2=0;
if( pet[j] == NULL || !strcmp(pet[j],"马") )
{
if( pet[j] == NULL ) isNull2 = 1;
if( isNull2 ) pet[j] = "马";
if( 0 == foo(level+1) ) return 0;
if( isNull2 ) pet[j] = NULL;
}
}
if( i+1<=4 )
{
int j = i+1;
int isNull2=0;
if( pet[j] == NULL || !strcmp(pet[j],"马") )
{
if( pet[j] == NULL ) isNull2 = 1;
if( isNull2 ) pet[j] = "马";
if( 0 == foo(level+1) ) return 0;
if( isNull2 ) pet[j] = NULL;
}
}
if( isNull1 ) smoke[i] = NULL;
}
return 1;
}
break;
case 12 :
{
int i = 0;
for( i = 0; i < 5; i ++ )
{
int isNull1=0;
if( smoke[i] != NULL && strcmp(smoke[i],"Blend") ) continue;
if( smoke[i] == NULL ) isNull1 = 1;
if( isNull1 ) smoke[i] = "Blend";
if( i-1>=0 )
{
int j = i-1;
int isNull2=0;
if( pet[j] == NULL || !strcmp(pet[j],"猫") )
{
if( pet[j] == NULL ) isNull2 = 1;
if( isNull2 ) pet[j] = "猫";
if( 0 == foo(level+1) ) return 0;
if( isNull2 ) pet[j] = NULL;
}
}
if( i+1<=4 )
{
int j = i+1;
int isNull2=0;
if( pet[j] == NULL || !strcmp(pet[j],"猫") )
{
if( pet[j] == NULL ) isNull2 = 1;
if( isNull2 ) pet[j] = "猫";
if( 0 == foo(level+1) ) return 0;
if( isNull2 ) pet[j] = NULL;
}
}
if( isNull1 ) smoke[i] = NULL;
}
return 1;
}
break;
case 13 :
{
int i = 0;
for( i = 0; i < 5; i ++ )
{
int isNull1=0;
if( people[i] != NULL && strcmp(people[i],"挪威") ) continue;
if( people[i] == NULL ) isNull1 = 1;
if( isNull1 ) people[i] = "挪威";
if( i-1>=0 )
{
int j = i-1;
int isNull2=0;
if( color[j] == NULL || !strcmp(color[j],"蓝色") )
{
if( color[j] == NULL ) isNull2 = 1;
if( isNull2 ) color[j] = "蓝色";
if( 0 == foo(level+1) ) return 0;
if( isNull2 ) color[j] = NULL;
}
}
if( i+1<=4 )
{
int j = i+1;
int isNull2=0;
if( color[j] == NULL || !strcmp(color[j],"蓝色") )
{
if( color[j] == NULL ) isNull2 = 1;
if( isNull2 ) color[j] = "蓝色";
if( 0 == foo(level+1) ) return 0;
if( isNull2 ) color[j] = NULL;
}
}
if( isNull1 ) people[i] = NULL;
}
return 1;
}
break;
case 14 :
{
int i = 0;
for( i = 0; i < 5; i ++ )
{
int isNull1=0;
if( drink[i] != NULL && strcmp(drink[i],"开水") ) continue;
if( drink[i] == NULL ) isNull1 = 1;
if( isNull1 ) drink[i] = "开水";
if( i-1>=0 )
{
int j = i-1;
int isNull2=0;
if( smoke[j] == NULL || !strcmp(smoke[j],"Blend") )
{
if( smoke[j] == NULL ) isNull2 = 1;
if( isNull2 ) smoke[j] = "Blend";
return 0;
}
}
if( i+1<=4 )
{
int j = i+1;
int isNull2=0;
if( smoke[j] == NULL || !strcmp(smoke[j],"Blend") )
{
if( smoke[j] == NULL ) isNull2 = 1;
if( isNull2 ) smoke[j] = "Blend";
return 0;
}
}
if( isNull1 ) drink[i] = NULL;
}
return 1;
}
break;
default :
break;
}
return 1;
}
温馨提示:内容为网友见解,仅供参考