實(shí)現(xiàn)基于整數(shù)的冪函數(shù)pow(int,int)的最有效方法在C中將一個整數(shù)提高到另一個整數(shù)的冪的最有效的方法是什么?// 2^3pow(2,3) == 8// 5^5pow(5,5) == 3125
3 回答

呼喚遠(yuǎn)方
TA貢獻(xiàn)1856條經(jīng)驗 獲得超11個贊
int ipow(int base, int exp){ int result = 1; for (;;) { if (exp & 1) result *= base; exp >>= 1; if (!exp) break; base *= base; } return result;}

拉丁的傳說
TA貢獻(xiàn)1789條經(jīng)驗 獲得超8個贊
2 ** 3 == 1 << 3 == 82 ** 30 == 1 << 30 == 1073741824 (A Gigabyte)
- 3 回答
- 0 關(guān)注
- 1669 瀏覽
添加回答
舉報
0/150
提交
取消