C + + Beginner---Outputs a continuous positive integer that may be represented, based on any positive integer entered

Source: Internet
Author: User

Title Description : A positive integer may be represented as a sum of n (>=2) consecutive positive integers, such as:

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

Write a program that identifies all consecutive positive integer sequences that meet this requirement, based on any of the positive integers you enter.

input data : A positive integer that is supplied to the program as a command-line argument.

output Data : Prints all positive integer sequences that match the title description on the standard output, one sequence per line, and each sequence starts from the smallest positive integer of the sequence and prints in small to large order. If the result has more than one sequence, the sequence is printed from small to large by the size of the smallest positive integer of each sequence. In addition, the sequence does not allow repetition, and integers within the sequence are separated by a space. If no sequence is met, the output is "NONE".

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

This is a 2005 Baidu Star Program Design contest questions preliminary question. Ideas are as follows:

1, meet the requirements of the number is continuous, so long as the selected starting value summation sum can be;

2. Ensure that all possible starting values are traversed and that the number of loops is minimized. Analysis you can know that a number is composed of at least two number sum, and because these two numbers are continuous. So the maximum starting value will not be greater than one-second of that number.

The code is as follows, VC6.0 verify OK. Please shoot the bricks, ^_^

 1 #include <iostream> 2 #include <vector> 3 4 using namespace Std;     5 6//According to any of the input positive integers, the output may be represented by a continuous positive integer 7 void Numbers (int number) 8 {9 if (number <= 0) {One return;12 }13 vector<int> save;15 bool exist = FALSE;16//traverse possible starting values for (int possible = 1; possible &lt ; NUMBER/2 + 1; possible++) * {int start = possible;20 int i = start;21 int sum = 0;22 while (Sum &                  lt;= number)//Save the contiguous positive integer that can be represented and output the sum of 27 + + = start;25 if (sum = = number) 26 exist = true;28 for (; i < start + 1; i++)                 Ave.push_back (i);}32 for (i = 0; i < save.size (); i++) 34 {cout << save[i] << "";}37 save.clear (); Empty, ready to save the next possible sequence cout << endl;39}40 start++;41}42}43 if (false = = exist) {cout < < "NONE" << endl;47}48}49 int main (int argc, char **argv)-{A. const int number = 21;53 Numbers (number); Numbers (+); return 0;57}

C + + Beginner---Outputs a continuous positive integer that may be represented, based on any positive integer entered

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.