Description: represents a positive integer as the sum of n (n> = 2) continuous positive integers, for example:
15 = 1 + 2 + 3 + 4 + 5
15 = 4 + 5 + 6
15 = 7 + 8
Enter any positive integer to find all consecutive positive integer sequences that meet this requirement.
If not found, the output is null.
Analysis: using the formula of the differential series, assuming that the input integer n, n = (A1 + A2) * (a2-a1 + 1)/<A1 <A2 <n.
Based on this method, it is not difficult to write the following:Code:
Void separateintoitems (long n) {If (n <= 2) {system. out. println ("null"); return;} double max = math. pow (2, 64); If (double) n> MAX) {system. out. println ("null"); return;} Boolean isexist = false; // A1 is the first numberfor (INT a1 = 1; A1 <= n/2; A1 ++) {// A2 is the last numberfor (INT a2 = A1 + 1; a2 <n; a2 ++) {// If exist output the resultif (A1 + A2) * (A2-A1 + 1) = N * 2) {isexist = true; SY Stem. out. print (A1); For (int t = A1 + 1; t <= a2; t ++) {system. out. print ("" + T);} system. out. println () ;}} if (! Isexist) {system. Out. println ("null ");}}
Reprinted please indicate the source of this article:Http://blog.csdn.net/love254443233