leetcode-cn.com/problems/cl…golang
一般,正整数 n 的阶乘是全部小于或等于 n 的正整数的乘积。例如,factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1。bash
相反,咱们设计了一个笨阶乘 clumsy:在整数的递减序列中,咱们以一个固定顺序的操做符序列来依次替换原有的乘法操做符:乘法(*),除法(/),加法(+)和减法(-)。app
例如,clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1。然而,这些运算仍然使用一般的算术运算顺序:咱们在任何加、减步骤以前执行全部的乘法和除法步骤,而且按从左到右处理乘法和除法步骤。less
另外,咱们使用的除法是地板除法(floor division),因此 10 * 9 / 8 等于 11。这保证结果是一个整数。ide
实现上面定义的笨函数:给定一个整数 N,它返回 N 的笨阶乘。函数
Normally, the factorial of a positive integer n is the product of all positive integers less than or equal to n. For example, factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1.ui
We instead make a clumsy factorial: using the integers in decreasing order, we swap out the multiply operations for a fixed rotation of operations: multiply (*), divide (/), add (+) and subtract (-) in this order.this
For example, clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1. However, these operations are still applied using the usual order of operations of arithmetic: we do all multiplication and division steps before any addition or subtraction steps, and multiplication and division steps are processed left to right.spa
Additionally, the division that we use is floor division such that 10 * 9 / 8 equals 11. This guarantees the result is an integer.设计
Implement the clumsy function as defined above: given an integer N, it returns the clumsy factorial of N.
解题思路: golang解 4个数字一组 每隔4循环调用f,f中除了小于等2 其余调用公式 i/*(i-1)/(i-2) + (i-3)*toggle,使用toggle是由于第一组四个的最后一个数字(i-3)是加,后面的都变成减,好比 10*9/8+7 - (6*5/4-3) 由于后面一组加了括号加号要转换成减号
func f(i int, toggle int) int {
if i <= 2 {
return i
}
return i*(i-1)/(i-2) + (i-3)*toggle
}
func clumsy(N int) int {
var res int
for i := N; i > 0; i -= 4 {
if i == N {
res = f(i, 1)
} else {
res -= f(i, -1)
}
}
return res
}
复制代码