嘿即時嘗試刷新我的思想與一些遞歸。 我想添加從'開始'到'結束'的所有數字。遞歸添加數字序列
也就是說,如果開始爲1,並最終爲5那麼答案將是1 + 2 + 3 + 4 + 5 = 15
到目前爲止,我已經有了這個
int calc(int start, int end){
if(start > end)
return total;
else{
total = total + start;
return sum1(start++, end);
}
}
其不工作(我得到seg故障)。我究竟做錯了什麼?
編輯:對不起,我在我的實際代碼中使用相同的變量,當我寫這個我結束了他們作爲開始/結束,並忘記更改所有的代碼。
切勿使用增量運營商時, 'start + 1'也可以。 – hugomg 2011-05-09 13:45:16