当前位置:网站首页>Leetcode:829. Sum of continuous integers

Leetcode:829. Sum of continuous integers

2022-07-01 03:32:00 Re:fused

subject :829. Sum consecutive integers

The question :

Given a positive integer n, return Continuous positive integers satisfy that the sum of all numbers is n Number of groups .

Answer key :

I thought of the equal deviation as 1 Equal difference sequence of , So there's a formula m a 1 + ( m − 1 ) m / 2 = m ( m a 1 + ( m − 1 ) / 2 ) = n ma_1+(m-1)m/2=m(ma_1+(m-1)/2) = n ma1+(m1)m/2=m(ma1+(m1)/2)=n, It can be seen that m Must be n My silver , also m-1 Even number , To ensure a 1 a_1 a1 Integers .

Code :

class Solution {
    
public:
    int consecutiveNumbersSum(int n) {
    
        int half = sqrt(n);
        int cn = 0;
        for(int i = 1; i <= half; i++){
    
            if(i == half && i*i == n){
    
                if((i-1)%2 == 0)cn++;
            }
            else{
    

            
            if(n % i == 0){
    
                if((i -1)%2 == 0)cn++;
                int other  = n / i;
                if((other-1)%2 == 0)cn++;
            }

            }
            
        }
        return cn;
    }
};


原网站

版权声明
本文为[Re:fused]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/182/202207010316404875.html