//2010/10/22 21:17:07 FRI
//用c++程序编写:
//在九宫格里填写数字,纵、横、斜(对角)相加=15
//create by jims
#include < iostream>
// +----+----+----+
// |1 |2 | 3 |
// +----+----+----+
// |4 |5 |6 |
// +----+----+----+
// |7 |8 |9 |
// +----+----+----+
// ADS:use emacs table-insert
bool check(int* a,int num)
{
//std::cout<<a[1]<<a[2]<<a[3]<<a[4]<<a[5]<<a[6]<<a[7]<<a[8]<<a[9]<<std::endl;
if( (a[1]+ a[2] +a[3] ==15)
&& (a[4] + a[5] + a[6] == 15)
&& (a[7] + a[8] + a[9] == 15)
&& (a[1] + a[4] + a[7] == 15)
&& (a[2] + a[5] + a[8] == 15)
&& (a[3] + a[6] + a[9] == 15)
&& (a[1] + a[5] + a[9] == 15)
&& (a[3] + a[5] + a[7] == 15)
)
{
for( int i = 1; i <= 9; i++)
{
for( int j = 1; j <= 9; j++)
{
if( i != j
&& a[i] == a[j])
return false;
}
}
return true;
}
else
return false;
}
int main(int argc, char* argv[])
{
int a[10];
bool find = false;
for( a[1] = 1; a[1] <= 9; a[1]++){
for( a[2] = 1; a[2] <= 9; a[2]++){
for( a[3] = 1; a[3] <= 9; a[3]++){
for( a[4] = 1; a[4] <= 9; a[4]++){
for( a[5] = 1; a[5] <= 9; a[5]++){
for( a[6] = 1; a[6] <= 9; a[6]++){
for( a[7] = 1; a[7] <= 9; a[7]++){
for( a[8] = 1; a[8] <= 9; a[8]++){
for( a[9] = 1; a[9] <= 9; a[9]++){
if( check( a,9))
{
find = true;
std::cout<<a[1]<<a[2]<<a[3]<<a[4]<<a[5]<<a[6]<<a[7]<<a[8]<<a[9]<<std::endl;
goto leble_find;
}
}}}}}}}}}
leble_find:
if( find)
{
std::cout<<a[1]<<" "<<a[2]<<" "<<a[3]<<"\n";
std::cout<<a[4]<<" "<<a[5]<<" "<<a[6]<<"\n";
std::cout<<a[7]<<" "<<a[8]<<" "<<a[9]<<"\n";
}
else
std::cout<<"can't find\n";
std::cin.get();
}