|
发表于 2008-4-15 20:26:38
|
显示全部楼层
#include <iostream>
using namespace std;
bool CanSet(int *const col, const int row)
{
for (int i=1; i<row; i++){
if (col == col[row]) return false;
}
return true;
}
void IsCheckedEqualEachLine(int *const col)
{
if (col[5]==1 ||col[5]==9)return ;
else
{
int A = 1000*col[9] + 100*col[8] + 10*col[7] + col[6];
int B = 1000*col[4] + 100*col[3] + 10*col[2] + col[1];
if (A*col[5] == B)
{
cout << A << " * " << col[5] << " = " << B << endl;
}
}
}
void AllPermutation(int *const col, const int n)
{
int row = 1;
col[1] = 0;
while (row > 0)
{
col[row]++; //右移一列
while (col[row]<=n && !CanSet(col,row))
{
col[row]++;
}
if (col[row] <= n)
{
if (row == n)//最后一行排完
{
IsCheckedEqualEachLine(col);
}
else
{
row++;
col[row] = 0;
}
}
else
{
row--;
}
}
}
void main()
{
int *const p = new int[10]; //p[0]未使用
AllPermutation(p,9);
delete[] p;
} |
|