围观,其实我什么也不懂。
#include<stdio.h> #include<stdlib.h> int combine(int n, int m, int a[], int b[], int temp); int main(void) { int k, save[52], i, j, len, tempsave[52], sum, flag = 0; int poke[52] ={ 13,13,13,13,12,12,12,12,11,11,11,11,10,10,10,10, 9,9,9,9,8,8,8,8,7,7,7,7,6,6,6,6,5,5,5,5,4,4,4,4, 3,3,3,3,2,2,2,2,1,1,1,1 }; for(k = 0; k <52; k++) save[k] = 0; k = 0;len = 1; while(k < 52){ j = len; while(j-1 && flag){ save[j-1] = save[j-2]; j--; } save[0] = poke[k++]; sum = 0; for(i = 1; i < len; i++, sum = 0){ sum = combine(len, i, save, tempsave, i); if(sum == 24){ break; } } if(sum == 24){ save[0] = 0; flag = 0; continue; } len++; flag = 1; } for(k = 0; k < len; k++) printf("共%d张牌,分别是%d\n", len, save[k]); } int combine(int n, int m, int a[], int b[], int temp) { int j, i, sum = 0; for( j=n; j>=m; j--){ b[m-1] = j-1; if(m>1){ sum = combine(j-1,m-1,a,b,temp);//用到了递归思想 if(sum == 24) return 24; }else{ sum = 0; for(i=temp-1;i>=0;i--) sum += a[b[i]]; if(sum == 24) return 24; } } }