Simple Loop nesting topics

Source: Internet
Author: User

Topic Description: A positive integer may be represented as the sum of N (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

#include "stdafx.h"

int _tmain (int argc, _tchar* argv[])
{
Try
{
int total = ATOL (argv[1]);
if (total <= 0)
{
printf ("NONE");
return 0;
}

int n = 1, n1, n2, SUM,BK = 1;

Loop 1: Start with the seed number n loop 2, n is the cyclic factor, the initial value is 1, n takes the maximum range does not exceed total
while (n < total)
{
Loop 2: Start the sum (m) operation from the seed number N1, N1 is the cyclic factor, the initial value is N, the N1,m value range is not greater than total,
Execute loop 3 by the conditional statement sum (m) = Total
N1 = n;
sum = 0;
while (N1 < total)
{
if (sum + = n1) = total)
{
Loop 3: Start the print operation from the seed number N2, N2 is the cyclic factor, the initial value is N, the N2 value range is not greater than N1
N2 = n;
while (N2 <= N1)
{
printf ("%d", N2);
n2++;
}

printf ("n");
BK = 0;
}
n1++;
}
n++;
}

if (BK) printf ("NONE");
}
catch (...) {}
return 0;
}

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.