-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathSum.cs
More file actions
44 lines (35 loc) · 879 Bytes
/
Copy pathSum.cs
File metadata and controls
44 lines (35 loc) · 879 Bytes
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
/*
题目名称:
求1+2+3+...+n
题目描述:
求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。
代码结构:
class Solution
{
public int Sum_Solution(int n)
{
// write code here
}
}
*/
using System;
namespace Sum {
class Solution {
/// <summary>
/// 解法
/// 基本思路:
/// 使用递归代替循环,使用逻辑与的短路特性来终止递归
/// </summary>
public int Sum_Solution(int n)
{
bool ret = n > 1 && ((n +=Sum_Solution(n - 1)) > 1);
return n;
}
public void Test() {
int n = 3;
n = 1;
n = 5;
Console.WriteLine(Sum_Solution(n));
}
}
}