#include <stdio.h>
#include <stdlib.h>
//把答案往前傳
int sum_1(int n){
if(n == 0){
return 0;
}else{
return n+sum_1(n-1);
}
}
//把答案往後傳
void sum_2(int n,int ans2){
if( n==0 ){
printf("遞迴二 1+2+...+10=%d\n",ans2
); return;
}else{
sum_2(n-1,ans2+n);
}
}
int main(int argc, char *argv[])
{
int ans1 =sum_1(10);
printf("遞迴一 1+2+...+10=%d\n",ans1
);
sum_2(10,0);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCi8v5oqK562U5qGI5b6A5YmN5YKzIAppbnQgc3VtXzEoaW50IG4pewogICAgaWYobiA9PSAwKXsKICAgICAgICByZXR1cm4gMDsKICAgIH1lbHNlewogICAgICAgIHJldHVybiBuK3N1bV8xKG4tMSk7ICAgICAgCiAgICB9Cn0KCi8v5oqK562U5qGI5b6A5b6M5YKzIAp2b2lkIHN1bV8yKGludCBuLGludCBhbnMyKXsKICAgIGlmKCBuPT0wICl7CiAgICAgICAgcHJpbnRmKCLpgZ7ov7TkuowgIDErMisuLi4rMTA9JWRcbiIsYW5zMik7CiAgICAgICAgcmV0dXJuOyAgICAgICAgIAogICAgfWVsc2V7CiAgICAgICAgc3VtXzIobi0xLGFuczIrbik7ICAgICAgCiAgICB9ICAgICAgCn0KCmludCBtYWluKGludCBhcmdjLCBjaGFyICphcmd2W10pCnsKICAKICAgIGludCBhbnMxID1zdW1fMSgxMCk7CiAgICBwcmludGYoIumBnui/tOS4gCAgMSsyKy4uLisxMD0lZFxuIixhbnMxKTsKICAKICAgIHN1bV8yKDEwLDApOwogICAgc3lzdGVtKCJQQVVTRSIpOwkKICAgIHJldHVybiAwOwp9Cg==