-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClumsyFactorial.java
More file actions
76 lines (68 loc) · 2.02 KB
/
ClumsyFactorial.java
File metadata and controls
76 lines (68 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package leetcode;
/**
* ClumsyFactorial
* https://leetcode-cn.com/problems/clumsy-factorial/
* 1006. 笨阶乘
* https://leetcode-cn.com/problems/clumsy-factorial/solution/mo-ni-jie-ti-by-oshdyr-bxtf/
*
* @since 2021-04-01
*/
public class ClumsyFactorial {
public static void main(String[] args) {
ClumsyFactorial sol = new ClumsyFactorial();
System.out.println(sol.clumsy(10));
System.out.println(sol.clumsy(1000));
System.out.println(sol.clumsy(100));
System.out.println(sol.clumsy(1));
System.out.println(sol.clumsy(2));
System.out.println(sol.clumsy(3));
System.out.println(sol.clumsy(4));
System.out.println(sol.clumsy(5));
System.out.println(sol.clumsy(6));
System.out.println(sol.clumsy(7));
System.out.println(sol.clumsy(8));
System.out.println(sol.clumsy(9));
}
public int clumsy(int N) {
int result = -1;
for (int next = N; next > 0; next -= 4) {
int curr = next;
if (next - 1 > 0) {
curr *= (next - 1);
} else {
if (result < 0) {
result = curr;
} else {
result = result - curr;
}
break;
}
if (next - 2 > 0) {
curr /= (next - 2);
} else {
if (result < 0) {
result = curr;
} else {
result = result - curr;
}
break;
}
if (next - 3 > 0) {
curr += (next - 3);
} else {
if (result < 0) {
result = curr;
} else {
result = result - curr;
}
break;
}
if (result < 0) {
result = curr;
} else {
result = result - curr + 2 * (next - 3);
}
}
return result;
}
}