| 作者 |
提上来,回答悠悠小草 [每日一题] 之 谁养鱼? |
 |
| 所跟贴 |
虽然我的答案是你结果中的第6个,不得不承认我是“不求甚解”啊 :-) -- 海蓝 - (23 Byte) 2005-4-19 周二, 02:20 (380 reads) |
书生 [博客] [个人文集]
游客
|
|
作者:游客 在 海归茶馆 发贴, 来自【海归网】 http://www.haiguinet.com
不能代表海归网的水平。
#include
#include
char strHouse[5][100] = { "Red", "White", "Blue", "Green", "Yellow" };
char strNation[5][100] = { "English", "Danish", "Norwegian", "Germany", "Swedish" };
char strSmoke[5][100] = { "PALL MALL", "Mixed", "DUNHILL", "BLUE MASTER", "PRINCE" };
char strDrink[5][100] = { "Tea", "Coffee", "Water", "Milk", "Beer" };
char strPet[5][100] = { "Cat", "Dog", "Fish", "Bird", "Horse" };
int Count[5] = {0,0,0,0,0};
int Num[5][5] =
{
{ 0, 1, 2, 3, 4 },
{ 0, 1, 2, 3, 4 },
{ 0, 1, 2, 3, 4 },
{ 0, 1, 2, 3, 4 },
{ 0, 1, 2, 3, 4 }
};
int Flag[5][5] =
{
{ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 }
};
int House = 0;
int Nation = 1;
int Smoke = 2;
int Drink = 3;
int Pet = 4;
int Block[5][5] =
{
{ 0, 1, 2, 3, 4 },
{ 0, 1, 2, 3, 4 },
{ 0, 1, 2, 3, 4 },
{ 0, 1, 2, 3, 4 },
{ 0, 1, 2, 3, 4 }
};
int GetIndex(int iType, int val)
{
int i;
for (i=0;i<5;i++)
if (Block[iType][i] == val)
return i;
}
int IsNeighbor(int i, int j)
{
if (i != j)
{
if (i + 1 == j || j + 1 == i)
return 1;
}
return 0;
}
int FoundOneMatch()
{
if (
GetIndex(Nation, 2) == 0 &&
GetIndex(Drink, 3) == 2 &&
GetIndex(House, 0) == GetIndex(Nation, 0) &&
GetIndex(Nation, 4) == GetIndex(Pet, 1) &&
GetIndex(Nation, 1) == GetIndex(Drink, 0) &&
GetIndex(House, 3) == GetIndex(Drink, 1) &&
GetIndex(Smoke, 0) == GetIndex(Pet, 3) &&
GetIndex(House, 4) == GetIndex(Smoke, 2) &&
GetIndex(Smoke, 3) == GetIndex(Drink, 4) &&
GetIndex(Nation, 3) == GetIndex(Smoke, 4) &&
GetIndex(House, 3) < GetIndex(House, 1) &&
IsNeighbor(GetIndex(Smoke, 1), GetIndex(Pet, 0)) &&
IsNeighbor(GetIndex(Pet, 4), GetIndex(Smoke, 2)) &&
IsNeighbor(GetIndex(Nation, 2), GetIndex(House, 2)) &&
IsNeighbor(GetIndex(Smoke, 1), GetIndex(Drink, 2))
)
return 1;
return 0;
}
void OutputMatch()
{
int i;
printf("\n");
// 1 2 3 4
// 01234567890123456789012345678901234567890123456789
printf("====================================================\n");
printf("House Nation Smoke Drink Pet\n");
printf("====================================================\n");
for (i=0; i<5; i++)
printf("%-8s %-8s %-12s %-8s %-8s\n",
strHouse[Block[0][i]],
strNation[Block[1][i]],
strSmoke[Block[2][i]],
strDrink[Block[3][i]],
strPet[Block[4][i]]);
printf("====================================================\n");
}
void GoThrough(int iType)
{
int number = 5;
int i, j;
Count[iType]++;
for(i=0;i
{
Num[iType][i]=i;
Flag[iType][i]=1;
printf("%d %d %d %d %d",
Count[0],
Count[1],
Count[2],
Count[3],
Count[4]);
}
while(1)
{
for(j=0;j
{
Block[iType][j] = Num[iType][j];
}
if (iType < 4)
{
//printf("%d %d %d %d %d %d\n",
// iType,
// Block[iType][0],
// Block[iType][1],
// Block[iType][2],
// Block[iType][3],
// Block[iType][4]);
GoThrough(iType + 1);
}
if (iType == 4)
{
//printf(".");
//printf("%d %d %d %d %d %d\n",
// iType,
// Block[iType][0],
// Block[iType][1],
// Block[iType][2],
// Block[iType][3],
// Block[iType][4]);
//printf("%d %d %d %d %d\n",
// Flag[iType][0],
// Flag[iType][1],
// Flag[iType][2],
// Flag[iType][3],
// Flag[iType][4]);
//OutputMatch();
if (FoundOneMatch())
OutputMatch();
}
Flag[iType][Num[iType][number-1]] = 0;
for(i=number-2;i>=0;i--)
{
Flag[iType][Num[iType][i]] = 0;
if(Num[iType][i]
}
if(i<0)
break;
for(j=Num[iType][i]+1;j
{
if(!Flag[iType][j])
break;
}
Flag[iType][j]=1;
Num[iType][i]=j;
for(j=0,i++;i
if(!Flag[iType][j])
{
Num[iType][i++]=j;
Flag[iType][j]=1;
}
};
}
int main(void)
{
GoThrough(0);
return 0;
}
作者:游客 在 海归茶馆 发贴, 来自【海归网】 http://www.haiguinet.com
|
|
|
| 返回顶端 |
|
 |
|
| |
|
|
|
您不能在本论坛发表新主题, 不能回复主题, 不能编辑自己的文章, 不能删除自己的文章, 不能发表投票, 您 不可以 发表活动帖子在本论坛, 不能添加附件不能下载文件, |
|
|