bzoj 3101 N皇后构造一种解 数学
3101: N皇后
Time Limit: 10 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 70 Solved: 32
[Submit][Status]
Description
n*n的棋盘,在上面摆下n个皇后,使其两两间不能相互攻击…
Input
一个数n
Output
第i行表示在第i行第几列放置皇后
Sample Input
4
Sample Output
2
4
1
3
4
1
3
HINT
100%的数据3<n<1000000。输出任意一种合法解即可
Source
题解:http://blog.csdn.net/nike0good/article/details/41006705
1 #include<cstring> 2 #include<cmath> 3 #include<iostream> 4 #include<algorithm> 5 #include<cstdio> 6 7 using namespace std; 8 9 int n; 10 11 int main() 12 { 13 scanf("%d",&n); 14 if (n%6!=2&&n%6!=3) 15 { 16 for (int i=2;i<=n;i+=2) 17 printf("%d\n",i); 18 for (int i=1;i<=n;i+=2) 19 printf("%d\n",i); 20 } 21 else 22 { 23 int k=n>>1; 24 if (k&1) 25 { 26 for (int i=k;i<=n-1;i+=2) 27 printf("%d\n",i); 28 for (int i=1;i<=k-1;i+=2) 29 printf("%d\n",i); 30 for (int i=k+3;i<=n;i+=2) 31 printf("%d\n",i); 32 for (int i=2;i<=k+1;i+=2) 33 printf("%d\n",i); 34 } 35 else 36 { 37 for (int i=k;i<=n;i+=2) 38 printf("%d\n",i); 39 for (int i=2;i<=k-1;i+=2) 40 printf("%d\n",i); 41 for (int i=k+3;i<=n-1;i+=2) 42 printf("%d\n",i); 43 for (int i=1;i<=k+1;i+=2) 44 printf("%d\n",i); 45 } 46 if (n&1)printf("%d\n",n); 47 } 48 }