传送门
nowcoder
leetcode
题目描述
求 1 + 2 + 3 + … + n,
要求:不能使用乘除法、for、while、if、else、switch、case 等关键字及条件判断语句。
C++ 代码 - nowcoder
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
|
class Solution { public: int Sum_Solution(int n) { int sum = n; bool res = (n > 0) && ((sum += Sum_Solution(n - 1)) > 0); return sum; } };
class Solution { public: int Sum_Solution(int n) { bool res[n][n + 1]; return sizeof(res) >> 1; } };
|
C++ 代码 - leetcode
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| class Solution { public: int mechanicalAccumulator(int n) { int sum = n; bool res = (n > 0) && ((sum += mechanicalAccumulator(n - 1)) > 0); return sum; } };
class Solution { public: int mechanicalAccumulator(int n) { bool res[n][n + 1]; return sizeof(res) >> 1; } };
|