The sum of continuous positive integers (East China Normal University OJ-3025)

Source: Internet
Author: User

Description: a positive integer may be expressed as the sum of n (n & gt; = 2) continuous positive integers, for example:

15 = 1 + 2 + 3 + 4 + 5 15 = 4 + 5 + 6 15 = 7 + 8

Write a program to find all consecutive positive integer sequences that meet the input positive integer.

Input data: A Positive Integer provided to the program as a command line parameter.

Output Data: all positive integer sequences that conform to the topic description are printed on the standard output. Each sequence is a sequence. Each sequence starts from the smallest positive integer of the sequence and is printed in ascending order. If the result has multiple sequences, print the smallest positive integers of each sequence from small to large. In addition, the sequence cannot be repeated. integers in the sequence are separated by a space. If there is no matching sequence, "NONE" is output ".

For example, for 15, the output result is: 1 2 3 4 5 5 6 7 8 for 16. The output result is: NONE.

 

 

Analysis:

N (1 + n)/2 <= m (set n to max) max = (int) sqrt (m) * sqrt (2) start = m/n-(n-1)/2 is an integer? If it is an integer, the condition is met, end = start + n, and the analysis above start to end is shown from: http://blog.chinaunix.net/u2/65855/showart_723252.html Program Implementation: # include <stdio. h> # include <math. h> int main () {int m = 0, n = 0, start = 0, end = 0, flag = 0; // m indicates the number of inputs, n is the number of positive integer sequences, start is the displayed start positive integer, end is the displayed end positive integer, flag indicates whether a sequence that meets the conditions exists, // 0 (default) No, 1 has float temp = 0.0; printf ("please input a Integer:"); scanf ("% d", & m); printf ("/n "); n = (int) (sqrt (m) * sqrt (2); // obtain the maximum number of sequences possible while (n> = 2) {temp = (float) m/n-(float) (n-1)/2; // evaluate the Starting number if (temp = (int) temp) {// determine whether it is a positive integer, that is, there are no matching sequences that exist for (flag = 1, start = (int) temp, end = start + n; start <end; start ++) {// flag is set to 1 and a sequence that meets the conditions exists. The starting integer start and ending integer end printf ("% d", start) are obtained ); printf ("");} printf ("/n");} n --;} if (flag = 0) // no matching sequence exists, output "none" printf ("none"); return 0 ;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.