※ 引述《sss955212 (靈魂的缺角)》之銘言:
: 1. Consider the following procedure.If the argument n is 5,what will the
: procedure return?
: procedure SUM(n:integer)
: {if n=1
: return(0)
: else
: return(SUM(n-1)+n*(n-1))
: }
: 答案是 24 嗎??
這題答案是40,同版上其他大大的解答。
下面是相對應的C++ Code參考,你可以拿回去編譯看看結果為何。
#include <iostream>
using namespace std;
int SUM(int);
int main(int argc, char *argv[]){
int n = 0;
int sum;
cout << "Enter number for n:";
cin >> n;
sum = SUM(n);
cout << "The answer is " << sum << endl;
exit(0);
}
int SUM(int n){
if (n==1)
{
return(0);
}
else
{
return(SUM(n-1) + n*(n-1));
}
}
--
空中精靈。SkyElf/Stevennick
要重新在空中飛翔‧遨遊全世界 未來正在我的手中 一點一滴的實現
My Blog: http://stevennick.dyndns.org/
My Podcasting: http://podcast.blog.webs-tv.net/skyelf
____________________________________________________________________________
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 219.68.140.151