1. 程式人生 > >leecode829+判斷一個數是否由一段連續數字求和,數學

leecode829+判斷一個數是否由一段連續數字求和,數學

https://leetcode.com/problems/consecutive-numbers-sum/description/

class Solution {
public:
    int consecutiveNumbersSum(int N) {
        int cnt = 0;
        for(int i=1; i<=N; i++){
            int a = i*(i-1)/2;
            if(a>=N) break;
            int num = N-a;
            if(num%i==0) cnt+=1;
        }
        return cnt;
    }
};