题目:求1+2+…+n,要求不能使用乘除法、for、while、ifelse、switch、case等关键字及条件判断语句(A?B:C)。
思路:采用递归,用&&代替if判断
class Solution {
public:
int Sum_Solution(int n) {
int ans = n;
ans&&(ans += Sum_Solution(n-1));
return ans;
}
};
博客提出求1+2+…+n的问题,要求不能使用乘除法、for、while、ifelse等关键字及条件判断语句。给出的思路是采用递归方法,并用&&代替if判断来解决该问题。
题目:求1+2+…+n,要求不能使用乘除法、for、while、ifelse、switch、case等关键字及条件判断语句(A?B:C)。
思路:采用递归,用&&代替if判断
class Solution {
public:
int Sum_Solution(int n) {
int ans = n;
ans&&(ans += Sum_Solution(n-1));
return ans;
}
};
4160

被折叠的 条评论
为什么被折叠?