[問題] 遞迴問題
( *[1m *[m 為色碼,可以按 Ctrl+V 預覽會顯示的顏色 )
( 未必需要依照此格式,文章條理清楚即可 )
遇到的問題: (題意請描述清楚)
想知道code如何run的
有問題的code: (請善用置底文標色功能)
#include <stdio.h>
struct stack {
int top;
int stk[20];
};
void show(int stk[],int i)
{ if (i>=0) { printf("%3d",stk[i]); show(stk,i-1); }
int q(int n,int k)
{
static stack s={-1};
if (n<k) return 0;
else if (n==k) {
printf("%3d",n);
show(s.stk,s.top);
printf("\n");
return 1;
} else {
int x=q(n,k+1); //此處開始混亂
s.top++; s.stk[s.top]=k; //k=??
int y=q(n-k,k);
s.top--;
return x+y;
}
}
int p(int n) { return q(n,1); }
void ui(void)
{
printf("Enter an integer <=20: ");
int n;
if (scanf("%d",&n)!=EOF) {
printf("There are %d ways to partition %d.\n\n",p(n),n);
ui();
}
}
int main(void) { ui(); }
程式其實就是integer partition
假設輸入6的話 q的function會怎麼跑呢
請問有人可以解答嗎 感謝!!
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.113.140.55
推
01/03 01:02, , 1F
01/03 01:02, 1F
→
01/03 01:02, , 2F
01/03 01:02, 2F
※ 編輯: wind760311 來自: 140.113.140.55 (01/03 01:16)
→
01/03 01:17, , 3F
01/03 01:17, 3F
※ 編輯: wind760311 來自: 140.113.140.55 (01/03 01:35)
※ 編輯: wind760311 來自: 140.113.140.55 (01/03 01:36)
推
01/03 11:17, , 4F
01/03 11:17, 4F
討論串 (同標題文章)