题解 | #求1+2+3+...+n#
求1+2+3+...+n
https://www.nowcoder.com/practice/7a0da8fc483247ff8800059e12d7caf1
class Solution { public: int Sum_Solution(int n) { // ret = n^2/2 + n / 2; double ret = pow(n, 2) + n; return (int) ret / 2; } };
求1+2+3+...+n
https://www.nowcoder.com/practice/7a0da8fc483247ff8800059e12d7caf1
class Solution { public: int Sum_Solution(int n) { // ret = n^2/2 + n / 2; double ret = pow(n, 2) + n; return (int) ret / 2; } };
相关推荐