Re: [理工] 資結請教
※ 引述《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)
討論串 (同標題文章)
完整討論串 (本文為第 2 之 2 篇):