zoukankan      html  css  js  c++  java
  • 输入一个正整数n,输出所有和为n的连续正整数序列

      public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            while (true) {
                System.out.print("please input a positive integer:");
                int n = sc.nextInt();
                if(n <= 2) break;
                int count = getSubInteger(n);
                
                System.out.println(n + "共有 " + count + " 种连续分解!");
                
                System.out.println("=========================");
            }
            sc.close();
        }
        
        public static int getSubInteger(int n) {
            int min = 1;
            int max = 1;
            int sum = 0;
            int count = 0;
            while (min <= (n-1) / 2 ) {
                if (sum == n) {
                    System.out.print(n + " = ");
                    for (int k = min; k < max; k++) {
                        if (k < max - 1)
                            System.out.print(k + " + ");
                        else
                            System.out.print(k);
                    }
                    System.out.println();
                    count++;
                    min++;
                    max = min;
                    sum = 0;
                } else if (sum > n) {
                    min++;
                    max = min;
                    sum = 0;
                } else {
                    sum = sum + max;
                    max++;
                }
            }
            return count;
        }
  • 相关阅读:
    [转]如何从无到有建立推荐系统
    sql语句查询重复值
    推荐系统开发中十个关键点整理
    mongodb中的副本集搭建实践
    Unicode对象
    5W1H
    Python中实现switch分支结构
    数据结构-跳跃表
    redis入门笔记(3)
    redis入门笔记(2)
  • 原文地址:https://www.cnblogs.com/archimedes-euler/p/9975782.html
Copyright © 2011-2022 走看看