当前位置: 代码迷 >> 综合 >> 李白打酒(递归)
  详细解决方案

李白打酒(递归)

热度:29   发布时间:2024-01-24 06:51:04.0

李白打酒(递归)
void func(int a, int b, int c)
{
static int i=0;
int j=15-a-b-1;
static char arr[15] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,'1'};

    if(a==0 && b==0){
int k;

        if(c == 1){
i++;
printf("%d\n", i);
for(k=0; k<15; ++k){
printf("%c ", arr[k]);
}
printf("\n");
}
return ;
}
if(a>0){
arr[j] = '2';
func(a-1,b,c*2);
}
if(b>0){
arr[j] = '1';
func(a,b-1,c-1);
}
}

int main(void)
{
clock_t start, stop;
double total_time;

    start = clock();

    func(5,9,2);

    stop = clock();
total_time = (double)(stop-start)/CLOCKS_PER_SEC;
printf("total used %f second\n", total_time);

    system("pause");
return 0;
}

  相关解决方案