C + + beginners based on any positive integer input, the output may be expressed as a continuous positive integer _c language

Source: Internet
Author: User

Topic Description: A positive integer may be represented as the 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 finds all sequential positive integer sequences that meet this requirement, based on any positive integer you enter.

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

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

For example, for 15, the output results are:

1 2 3 4 5
4 5 6
7 8

For 16, the output results are:

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 as long as the selection of the starting value added sum can;

2. Be sure to traverse all possible starting values and make the loop as few times as possible. As you can see, a number is composed of at least two numbers, and because these two numbers are contiguous. So the maximum starting value is not greater than one-second of the number.

The code is as follows, VC6.0 verify OK. Please take a brick, ^_^

#include <iostream>
#include <vector>
using namespace std;
The output may be represented as a continuous positive integer
void Numbers (int number)
{
if (number <=)
{return
}
, depending on any positive integer entered; Vector<int> Save;
BOOL exist = FALSE;
Traversal possible starting values for
(int possible =; possible < number/+; possible++)
{
int start = possible;
int i = start;
int sum =;
while (sum <= number)//save a contiguous positive integer that can be represented and output
{
sum = start;
if (sum = = number)
{
exist = true;
for (; i < start +; i++)
{
save.push_back (i);
}
for (i =; I < save.size (); i++)
{
cout << save[i] << "";
}
Save.clear (); Empty, ready to save the next possible sequence
cout << endl;
}
start++
}
}
if (false = = exist)
{
cout << "NONE" << Endl
}
}
int main (int argc, char **argv)
{
const int number =;
Numbers (number);
Numbers ();
return;
}

The above is a small set to introduce the C + + beginners According to the input of any positive integer, the output may be expressed in consecutive positive integers, hope to help everyone!

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.