题目链接:求1+2+3+...+n
题意:
求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。
解题思路:
高中等差数列公式, n * (n + 1) / 2
class Solution {
public:
int Sum_Solution(int n) {
return n * (n + 1)/ 2;
}
};
本文介绍了一种不使用常见编程关键字和条件判断语句的等差数列求和方法,利用高中数学知识,通过直接计算公式n*(n+1)/2实现1+2+3+...+n的求和。
题目链接:求1+2+3+...+n
题意:
求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。
解题思路:
高中等差数列公式, n * (n + 1) / 2
class Solution {
public:
int Sum_Solution(int n) {
return n * (n + 1)/ 2;
}
};

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