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()));
}
}
}
心之所向,素履以往 生如逆旅,一苇以航

浙公网安备 33010602011771号