Skip to content

Commit eaa9036

Browse files
committed
Create 1006-clumsy-factorial.rs
1 parent e8a2e74 commit eaa9036

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

1006-clumsy-factorial.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
pub 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+
21+
fn main() {
22+
println!("{:?}", clumsy(2));
23+
}

0 commit comments

Comments
 (0)