Natural Number split

Source: Internet
Author: User

Question:

GivenSum,Min,MaxAndNFour positive integers. Please output allSumSplitNAn ascending positive integer (equal allowed), where each positive integerKAll meet the following requirements:Min <= k <= max.

 

InShao Xia's blogSeeThis question. Question and OutputNCountMAll combinations of numbers are similar, but only limitedMNumber and value range. You can use greedyAlgorithmConstruct the smallest combination, and then adjust the combination to get the next combination. Obviously, you should find a number from the back to the front to Increase the number.1, And then adjust the number after this number. This number is:The difference between the first and last number from the back to the front is greater1Number(Because if the difference value is smaller than or equal1, Cannot adjust the following number to get a combination that meets the requirements ).

// Www.cnblogs.com/flyinghearts

# Include < Iostream >
# Include < Vector >
# Include < Cassert >
Using STD: cout;
Using STD: vector;

Static Inline Void Print ( Int Arr [], Int Len, Int N)
{
Cout < N <   " = " ;
For ( Int I =   0 ; I < Len -   1 ; ++ I) cout < Arr [I] <   " + " ;
Cout < Arr [Len -   1 ];
Cout <   " \ N " ;
}

Static Inline Void Init ( Int Arr [], Int Len, Int Sum, Int Min, Int Max)
{
Sum -= Min * Len;
Int I = Len;
While (Sum -= Max - Min) >   0 ) Arr [ -- I] = Max;
Arr [ -- I] = Sum + Max;
While (I >   0 ) Arr [ -- I] = Min;
}

Void Solve ( Int Sum, Int N, Int Min, Int Max)
{
If (N <=   1   | Min > Max | Min <=   0   | Sum < N * Min | Sum > N * Max) Return ;
Static Vector < Int > V;
V. Resize (N );
Int   * First =   & V [ 0 ];
Int   * Last =   & V [n -   1 ];
Init (first, N, sum, Min, max );
While ( 1 ){
Print (first, N, sum );
Int   * P = Last -   1 ;
// If the difference between the last two numbers is greater than or equal to 2, adjust them directly.
If ( * Last -   * P > =   2 ){ ++ * P; --* Last; Continue ;}
Int TT =   * Last -   2 ;
Int SS =   * Last +   * P;
// From the back to the front, find the number with a big difference of 2 from the first to the last number, and adjust it from this number.
While ( -- P > = First &&   * P > TT) {SS + =   * P ;}
If (P < First) Break ;
// If (* P = TT & * last! = * (Last-1) {++ * P; -- * last; continue ;}
++ * P;
Init (P +   1 , Last - P, SS -   1 , * P, max );
}
Cout <   " \ N " ;
}

IntMain ()
{
Solve (10,4,1,5);
Solve (10,5,2,2);
}

 

 

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.