作者stevennick (空中精灵小金刚)
看板TransCSI
标题Re: [问题] 元智91年的转学考考古题
时间Mon May 28 21:31:37 2007
※ 引述《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