That was the 8 queen question, the recursive way of writing, and then this can ask the N queen to put the problem, using the purely mathematical method ... Magic!
#include <iostream> #include <cstdio> #include <fstream> #include <algorithm> #include < cmath> #include <deque> #include <vector> #include <list> #include <queue> #include < string> #include <cstring> #include <cstdlib> #include <map> #include <stack> #include < set> #define PI ACOs ( -1.0) #define MEM (A, B) memset (A,b,sizeof (a)) #define SCA (a) scanf ("%d", &a) #define SC (A, B) scanf ("%d%d", &a,&b) #define PRI (a) printf ("%d\n", a) #define Lson i<<1,l,mid#define Rson I<<1|1,mid +1,r#define MM 1000005#define MN 2005#define INF 100004#define eps 1e-7using namespace std;typedef long long ll;//output is the current column Which line needs to place the Queen//For example: 5 3 1 6 8 2 4 7//that is, the first column of the fifth row placed Queen, the second column of the third row placed Queen void Solvequeen (int n)//n Queen question {int k,i,first=1; if (n%6!=2&&n%6!=3) {for (i=2;i<=n;i+=2) printf ("%d", I); for (i=1;i<=n;i+=2) printf ("%d", I); } else {K=N/2; if (! ( k&1)) {for (i=k;i<=n;i+=2) printf ("%d", I); for (i=2;i<=k-2;i+=2) printf ("%d", I); for (i=k+3;i<=n-1;i+=2) printf ("%d", I); for (i=1;i<=k+1;i+=2) printf ("%d", I); if (n&1) printf ("%d", n); } else {for (i=k;i<=n-1;i+=2) printf ("%d", I); for (i=1;i<=k-2;i+=2) printf ("%d", I); for (i=k+3;i<=n;i+=2) printf ("%d", I); for (i=2;i<=k+1;i+=2) printf ("%d", I); if (n&1) printf ("%d", n); }} puts ("");} int main () {int n; while (SCA (n) &&n) Solvequeen (n); return 0;}