The classmate asked a question long ago, did it and then forgot to tell others. In the last few days, I suddenly thought of it and pulled it out.
It's done in a retrospective way.
There are many repetitions in the results, so each combination is output only once
/* Challenge: Put the number 1-13 into three buckets, and the difference between any two numbers in any bucket is not in the same bucket as these two numbers. (For example, if you put the numbers 5 and 7 in a bucket, you cannot put the number 2 in the same bucket) below is the solution: (text color is white, check the next line to see) [[2, 3, 7, 11, 12], [5, 6, 8, 9], [1, 4, 10, 13]] or [[2, 3, 11, 12], [5, 6, 7, 8, 9], [1, 4, 10, 13]]*/#include <iostream> #include <cstring>using namespace std;int a[13]={1,2,3,4,5,6,7,8,9,10,11,12,13};int a1[13],a2[13],a3[13];int n1,n2,n3,count;bool iscorrect (Const int *ax,const int c,const int num) {int i,j,sub;if ( -1==c) return true;for (i=0;i<=c;i++) {Sub=num-ax[i] ; for (j=0;j<=c;j++) if (ax[j]==sub) Return false;} Return true;} Void fill (int n) {if (14==n&&1==a1[0]&&2==a2[0]) {/*++count;cout<< "Case " <<count<< ":" <<endl;*/int tn1,tn2,tn3;tn1=tn2=tn3=0;++count;cout<< "case " < <count<< ":" <<endl;//output a1,a2,a3while (tn1<=n1) {cout<<a1[tn1++]<< " ";} Cout<<endl;while (TN2<=N2) {cout<<a2[tn2++]<< " ";} Cout<<endl;while (TN3<=N3) {cout<<a3[tn3++]<< " ";} Cout<<endl;return;} if (Iscorrect (a1,n1,n)) {//cout<<n<< "&NBSP;A1" <<endl;a1[++n1]=n; Fill (n+1); a1[n1]=0;n1--;} if (Iscorrect (a2,n2,n)) {//cout<<n<< "&NBSP;A2" <<endl;a2[++n2]=n; Fill (n+1); a2[n2]=0;n2--;} if (Iscorrect (a3,n3,n)) {//cout<<n<< " a3" <<endl;a3[++n3]=n; Fill (n+1); a3[n3]=0;n3--;} return;} Int main () {memset (a1,0,sizeof (A1)); Memset (a2,0,sizeof (A2)); Memset (a3,0,sizeof (A3)); n1=n2=n3=-1;count=0; Fill (1); return 0;}
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6C/1E/wKioL1VAhf2zWIoMAAD9JLGE7Tg284.jpg "title=" 1.jpg " alt= "Wkiol1vahf2zwiomaad9jlge7tg284.jpg"/>
This article is from the "hacker" blog, make sure to keep this source http://anglecode.blog.51cto.com/5628271/1640337
21 challenging questions, a few cups of coffee time, to try