Http://blog.csdn.net/csy981848153/article/details/7650100
Question: divide the numbers 1, 2, 3, 4, 5, 6, 7, 8, and 9 into three groups. Each number can only be used once, that is, the three numbers in each group cannot have repeated numbers, nor must they be repeated with the three numbers in other groups. The three numbers in each group must constitute a sequence number.
// Question: divide the numbers 1, 2, 3, 4, 5, 6, 7, 8, and 9 into three groups. Each number can only be used once, // that is, the three numbers in each group cannot have duplicate numbers, nor must they be repeated with the three numbers in other groups. The three numbers in each group must constitute a unique number. First, calculate the three digits that do not contain 0 and are the three digits of an integer square. Then, combine the three digits that meet the conditions. // By as1138 2011-06-21 # include <iostream> using namespace STD; int main (void) {int TEM = 0; int A [21] [3] = {0 }; int tval [3] = {0}; int n = 0; int itest [9] = {0}; For (INT I = 11; I! = 32; ++ I) {TEM = I * I; for (Int J = 2; j> = 0; -- j) {tval [J] = TEM % 10; TEM/= 10 ;} if (tval [0] = tval [1] | tval [1] = tval [2] | tval [0] = tval [2]) continue; a [n] [0] = tval [0]; a [n] [1] = tval [1]; a [n] [2] = tval [2]; + + N ;}// for (INT I = 0; I! = N; ++ I) // {// cout <A [I] [0] <A [I] [1] <A [I] [2] <Endl; //} For (int K = 0; k! = N-2; ++ K) {for (int t = k + 1; t! = N-1; ++ t) {for (int m = t + 1; m! = N; ++ m) {itest [A [k] [0]-1] = 1; itest [A [k] [1]-1] = 1; itest [A [k] [2]-1] = 1; itest [A [T] [0]-1] = 1; itest [A [T] [1]-1] = 1; itest [A [T] [2]-1] = 1; itest [A [m] [0]-1] = 1; itest [A [m] [1]-1] = 1; itest [A [m] [2]-1] = 1; for (INT I = 0; I! = 9; ++ I) {If (itest [I]! = 1) break; if (I = 8) {cout <A [k] [0] <A [k] [1] <A [k] [2] <"" <A [T] [0] <A [T] [1] <A [T] [2] <"" <A [m] [0] <A [m] [1] <A [m] [2] <Endl ;}} // end for (int I) for (Int J = 0; J! = 9; ++ J) {itest [J] = 0 ;}// end for (INT m)} // end for (INT t )} // end for (int K) return 0 ;}
Similar to my idea, I use backtracing + pruning, which is faster than N ^ 3.
The last digit of the complete number must be 1, 4, 5, 6, and 9.
Therefore, the fully calculated number of shards is classified by the last number.
Change the problem scale from N to n/2.