主要是要看哪样简单?
难得写的话说下思路就可以了.
谢谢.
下面是我第一次做出的结果,却觉得太复杂了.
#include<stdio.h>
char row(long n);
char xie1(long n);
char xie2(long n);
void prqueen(long n);
void main()
{
long n;
int count=0;
for(n=0;n<(1<<24);n++)
{
if(row(n))continue;
if(xie1(n))continue;
if(xie2(n))continue;
prqueen(n);
count++;
}
printf("There is %d answers in total.!\n",count);
}
char row(long n) //计算每行的数都不相等,有相等的则返回1;
{
int a1,a2,a3;
for(a1=0;a1<8;a1++)
{ a3=0;
for(a2=0;a2<8;a2++)
{
if(a1==((n&((1<<3*(a2+1))-(1<<3*a2)))>>(3*a2)))a3++;
if(a3>1)break;
}
if(a3>1)break;
}
if(a3>1)return (1);
else return (0);
}
char xie1(long n) //计算向左上斜的斜线上的最多存在一个数。否则返回1;
{
int b1,b2,j;
char a[15]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
for(b1=0;b1<8;b1++)
{
b2=((n&((1<<3*(b1+1))-(1<<3*b1)))>>(3*b1));
j=7+b1-b2;
a[j]+=1;
if(a[j]>1)break;
}
return(a[j]>1);
}
char xie2(long n) //计算向右上斜的斜线上的最多存在一个数。否则返回1;
{
int b1,b2,j;
char a[15]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
for(b1=0;b1<8;b1++)
{
b2=( (n&((1<<3*(b1+1))-(1<<3*b1)))>>(3*b1) );
j=b1+b2;
a[j]+=1;
if(a[j]>1)break;
}
return(a[j]>1);
}
void prqueen(long n) //打印。
{
int i,j;
char queen[8][8]={
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0}};
for(j=0;j<8;j++)
{
i= ( (n&((1<<3*(j+1))-(1<<3*j)))>>(3*j) );
queen[i][j]=1;
}
printf("*******************\n");
for(i=0;i<8;i++)
{
printf("* ");
for(j=0;j<8;j++)
{
if(queen[i][j]==1)printf("Q ");
else printf(" ");
}
printf("*\n");
}
printf("*******************\n");
return;
}