題目鏈接:求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;
}
};
題目鏈接:求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;
}
};