1. 程式人生 > >求1+2+3...+n之和

求1+2+3...+n之和

1.math.pow

class Solution {
public:
    int Sum_Solution(int n) {
        return ((int)pow(n,2)+n)>>1;
    }
};

2.遞迴. &短路

class Solution {
public:
	int Sum_Solution(int n) {
		int sum=n;
		bool res = sum && (sum += Sum_Solution(--n));
		return sum;
	}
};