829. 连续整数求和

给定一个正整数 N,试求有多少组连续正整数满足所有数字之和为 N?

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/consecutive-numbers-sum
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

麦麦麦麦子。

import java.util.Scanner;

class Solution {

    public int consecutiveNumbersSum(int n) {
        int ret = 0;
        int i = 1;
        while (n > 0) {
            ret += n % i == 0 ? 1 : 0;
            n -= i;
            i++;
        }
        return ret;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            System.out.println(new Solution().consecutiveNumbersSum(in.nextInt()));
        }
    }
}
posted @ 2021-12-15 10:41  Tianyiya  阅读(49)  评论(0)    收藏  举报