Daily brushing title (xxvii)
Blue Bridge Cup sixth C language group B match exercises Province
Problem E: an array of nine points
Test sites: the whole arrangement, backtracking, recursion
If on the recursive algorithm can be found in not very skilled Bowen algorithm a: recursively (including full array)
This code, test function a look that is truly the result of 1/3, and the function f is a look at a recursive call function, if the subject has done a lot of people to see that this is all arranged, because the loop from 0 9, and then swap to full permutation number k + 1 to 9, followed then swap back. So this question is not to say, I can see the details of the above recommended Bowen, the completed code is as follows:
#include <stdio.h>
void test(int x[])
{
int a = x[0]*1000 + x[1]*100 + x[2]*10 + x[3];
int b = x[4]*10000 + x[5]*1000 + x[6]*100 + x[7]*10 + x[8];
if(a*3==b) printf("%d / %d\n", a, b);
}
void f(int x[], int k)
{
int i,t;
if(k>=9){
test(x);
return;
}
for(i=k; i<9; i++){
{t=x[k]; x[k]=x[i]; x[i]=t;}
f(x,k+1);
t=x[k]; x[k]=x[i]; x[i]=t; // 填空处
}
}
int main()
{
int x[] = {1,2,3,4,5,6,7,8,9};
f(x,0);
return 0;
}