作者sunneo (艾斯寇德)
看板Grad-ProbAsk
标题Re: [理工] 资结请教
时间Fri Apr 3 01:07:21 2009
※ 引述《christensen ()》之铭言:
: 1. For the following recursive procedure RP1, (a) find RP1 (2468) = ?
: (b)how many times for executing the instruction print (key mod 10) in (a) ?
: Procedure RP1 (key: integer)
: begin
: if key < 10
: then print (key);
: else
: begin
: print (key mod 10);
: RP1 (key div 10)
: end
: end
3
print key mod 10 = 8 ---------- 1st
RP1(246)
print key mod 10 = 6 ---------- 2nd
RP1(24)
print key mod 10 = 4 ---------- 3rd
RP1(2)
print 2
: 2. Considering the following program, write its recursive program.
: long combi (int n, int r){
; int i;
: long p = 1;
: for ( i = 1; i≦r; i++)
: p = p*(n – i + 1)/i;
: return (p) ;
: }
这个程式就是combination,缩写为combi
典型的递回式是
long combi(int n,int r){
if(n == r || n == 0 || r == 0) return 1;
else if(r == 1) return n;
else return combi(n-1,r) + combi(n-1,r-1);
}
: 3. f(x) = 2x^4 + 3x^3 + 5x^2 + 8x + 9,
: what is the least number of multiplications
: required for evaluating f(x) ?
算出
x --------------- 0
x^2 --------------- 1
x^4 --------------- 1
8x --------------- 1
5x^2 --------------- 1
3x^3 = 3.x.x^2 ------- 2
2x^4 --------------- 1
共 7个
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 61.227.116.68
※ 编辑: sunneo 来自: 61.227.116.68 (04/03 01:08)