-
Notifications
You must be signed in to change notification settings - Fork 21
/
_064_N_Num_Sum.java
52 lines (45 loc) · 1.08 KB
/
_064_N_Num_Sum.java
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
package offerV2;
/**
* @No v2-064
* @problem 求前N个正整数之和
* @tag 递归
* @author liyazhou
* @date 2017-06-03
*
* <pre>
* 求 1+2+...+n。要求不能使用乘除法、for、while、if、else、switch、case等关键字
* 及条件判断语句。
* </pre>
*/
public class _064_N_Num_Sum {
/**
* Note
*
* Thought
*
* Algorithm
* 1.
* 2.
* 3.
*/
private static class Solution {
public static int sumOfTopN(int n) {
if (n == 1) {
return n;
} else {
return sumOfTopN(n-1)+n;
}
}
public static void main(String[] args) {
int result = sumOfTopN(10);
System.out.println("result = " + result);
int ret2 = sum(10);
System.out.println("ret2 = " + ret2);
}
public static int sum(int n) {
int sum = n;
boolean bool = (n > 0) && ((sum += sum(n-1)) > 0);
return sum;
}
}
}