输入一个正整数n,输出所有和为n的连续正整数序列
1 public static void main(String[] args) {
2 Scanner sc = new Scanner(System.in);
3 while (true) {
4 System.out.println("please input a positive integer: ");
5 int n = sc.nextInt();
6 getSubInteger(n);
7 System.out.println("=========================");
8 }
9 }
10
11 public static void getSubInteger(int n) {
12 int min = 1;
13 int max = 1;
14 int sum = 0;
15 while (min <= n / 2 + 1) {
16 if (sum == n) {
17 System.out.print(n + " = ");
18 for (int k = min; k <= max - 1; k++) {
19 if (k < max - 1)
20 System.out.print(k + " + ");
21 else
22 System.out.print(k);
23 }
24 System.out.println();
25 min++;
26 max = min;
27 sum = 0;
28 } else if (sum > n) {
29 sum = 0;
30 min++;
31 max = min;
32 } else {
33 sum = sum + max;
34 max++;
35 }
36 }
37 }
posted @
2018-11-17 21:44
书书数数 阅读(
...) 评论(
...)
编辑
收藏
最后
以上就是耍酷招牌最近收集整理的关于输入一个正整数n,输出所有和为n的连续正整数序列 输入一个正整数n,输出所有和为n的连续正整数序列 的全部内容,更多相关输入一个正整数n,输出所有和为n的连续正整数序列内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复