数值的整数次方 递归

2022/4/30 6:15:24

本文主要是介绍数值的整数次方 递归,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

 

https://leetcode-cn.com/problems/shu-zhi-de-zheng-shu-ci-fang-lcof/

func myPow(x float64, n int) float64 {
	var R func(x float64, m int) float64
	R = func(x float64, m int) float64 {
		if m == 0 {
			return 1
		}
		n := m % 2
		h := R(x, m/2)
		if n == 0 {
			return h * h
		} else {
			return h * h * x
		}
	}
	if n >= 0 {
		return R(x, n)
	} else {
		return 1 / R(x, -n)
	}
}

  

 



这篇关于数值的整数次方 递归的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程