[問題] a的b次方實作時間logb之遞迴寫法

看板C_and_CPP作者 (人生就是不停的戰鬥)時間15年前 (2010/03/24 15:10), 編輯推噓1(103)
留言4則, 4人參與, 最新討論串1/6 (看更多)
a的b次方計算時間在logb內完成 這題好像之前有在板上看過,但好像沒有遞迴版的 非遞迴的寫法我自己寫的如下: #include<iostream.h> using namespace std; int fastpow(int ,int ); int main() { int num=0, pow=0; cout<<"Enter num:"; cin>>num; cout<<"Enter pow:"; cin>>pow; cout<<fastpow(num,pow); system("pause"); } int fastpow(int a,int b) { int temp=1; while(b!=0) { if(b&1) {temp=temp*a;} a=a*a; b=b>>1; } return temp; } ================================= 造裡說非遞迴出的來〝遞迴〞應該就出的來 無奈可能非遞迴沒寫得很好,遞迴我實在想不出來要怎麼寫才漂亮 請教大家 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 58.114.69.46

03/24 23:47, , 1F
我想問的是,乘法運算該視為常數嗎?
03/24 23:47, 1F

03/24 23:53, , 2F
看你是乘常數次還是會隨輸入增減
03/24 23:53, 2F

03/24 23:58, , 3F
我更想知道要怎麼要在O(logN)算出Cn取r
03/24 23:58, 3F

03/25 21:30, , 4F
這個做出來好像會差a倍~~
03/25 21:30, 4F
文章代碼(AID): #1BgYjCaI (C_and_CPP)
討論串 (同標題文章)
文章代碼(AID): #1BgYjCaI (C_and_CPP)