和为S的正整数序列

来源:互联网 发布:ummyvideo mac 破解版 编辑:程序博客网 时间:2024/06/16 05:56
import java.util.Scanner;public class Main {    public static void main(String[] args) {        Scanner scan = new Scanner(System.in);        while (true) {            int N = scan.nextInt();            int M = scan.nextInt();            if (N == 0 && M == 0) {                break;            }            int small = 1, big = 1;            int curSum = small;            while (big <= M) {                if (curSum == M) {                    System.out.println("[" + small + "," + big + "]");                }                while (curSum > M) {                    curSum -= small;                    small++;                    if (curSum == M) {                        System.out.println("[" + small + "," + big + "]");                    }                }                big++;                curSum += big;            }        }    }}
import java.util.Scanner;public class Main {    public static void main(String[] args) {        Scanner scan = new Scanner(System.in);        while (true) {            int N = scan.nextInt();            int M = scan.nextInt();            if (N == 0 && M == 0) {                break;            }            int n = (int)Math.sqrt(M*2);    // 最大有n个数的和            while (n > 0) {                int a0 = (M-(n-1)*n/2)/n;                if (a0*n + (n-1)*n/2 == M) {    // 从0到n-1,而不是从1到n                    System.out.println("[" + a0 + "," + (a0+n-1) + "]");                }                n--;            }            System.out.println();        }        scan.close();    }}
0 0
原创粉丝点击