Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions leetcode/1001-1100/1006.Clumsy-Factorial/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
# [1006.Clumsy Factorial][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
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`.

We make a **clumsy factorial** using the integers in decreasing order by swapping out the multiply operations for a fixed rotation of operations with multiply `'*'`, divide `'/'`, add `'+'`, and subtract `'-'` in this order.

- 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.

Additionally, the division that we use is floor division such that `10 * 9 / 8 = 90 / 8 = 11`.

Given an integer `n`, return the clumsy factorial of `n`.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: n = 4
Output: 7
Explanation: 7 = 4 * 3 / 2 + 1
```

## 题意
> ...

## 题解
**Example 2:**

### 思路1
> ...
Clumsy Factorial
```go
```

Input: n = 10
Output: 12
Explanation: 12 = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1
```

## 结语

Expand Down
28 changes: 26 additions & 2 deletions leetcode/1001-1100/1006.Clumsy-Factorial/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(n int) int {
ans := 0
first := true
for ; n > 0; n -= 4 {
cur := n
if n-1 >= 1 {
cur *= (n - 1)
}
if n-2 >= 1 {
cur /= (n - 2)
}
if n-3 >= 1 {
if first {
cur += n - 3
} else {
cur -= n - 3
}
}
if first {
ans = cur
first = false
continue
}
ans -= cur
}
return ans
}
13 changes: 6 additions & 7 deletions leetcode/1001-1100/1006.Clumsy-Factorial/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", 4, 7},
{"TestCase2", 10, 12},
}

// 开始测试
Expand All @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading