MCPcopy Create free account
hub / github.com/StudyRust/leetcode_rust / clumsy

Function clumsy

1006-clumsy-factorial.rs:1–19  ·  view source on GitHub ↗
(n: i32)

Source from the content-addressed store, hash-verified

1pub fn clumsy(n: i32) -> i32 {
2 if n == 1 { return 1 }
3 if n == 2 { return 2 }
4 let mut ret = n * (n-1) / (n-2) + n-3;
5 let mut tmp = 0;
6 for (i, num) in (1..=n-4).rev().enumerate() {
7 match i % 4 {
8 0 => { tmp = num },
9 1 => { tmp *= num },
10 2 => {
11 ret -= (tmp as f64 / num as f64).floor() as i32;
12 tmp = 0
13 },
14 3 => { ret += num },
15 _ => { },
16 }
17 }
18 ret - tmp
19}
20
21fn main() {
22 println!("{:?}", clumsy(2));

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected