[問題] a的b次方實作時間logb之遞迴寫法
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
03/24 23:58, 3F
推
03/25 21:30, , 4F
03/25 21:30, 4F
討論串 (同標題文章)
以下文章回應了本文 (最舊先):
完整討論串 (本文為第 1 之 6 篇):