CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 19

Threaded View

  1. #15
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: How to distribute a value into vector

    Quote Originally Posted by Ramees219 View Post
    I didn't use the zero combination because i need this code run faster
    Again, you can just add 1 to every value. Also, the code provided has "int minimum = 1; // included" explicitly provided. Did you decide to stop reading after the first sentence?
    Quote Originally Posted by Ramees219 View Post
    But since #post 10 i realized its still take lost of time on big value
    So redesign my algorithm now i don't want zero combination and dual character in one array like .10, 11, 12 .. so on
    I'm not sure my previous algorithm can be easily adapted. But at this point, you might as well just programmatically generate the combinations you want. With recursion, it's actually a pretty simple (and efficient) algorithm.

    Code:
    #include <iostream>
    #include <iterator>
    #include <vector>
    #include <algorithm>
    
    int minimum = 1; // included
    int maximum = 9; // included
    int slots = 3;
    int sum = 17;
    
    template <typename ForwardIterator>
    void increment(ForwardIterator first, ForwardIterator it, ForwardIterator last, int remainder) {
      int free_slots = std::distance(it, last) - 1;
      if (free_slots == 0) {
        *it = remainder;
        std::copy(first, last, std::ostream_iterator<int>(std::cout, " "));
        std::cout << std::endl;
        return;
      }
    
      auto low = std::max(minimum, remainder - free_slots * maximum);
      auto high = std::min(maximum, remainder - free_slots * minimum);
    
      auto next = std::next(it);
      for (int i = low; i <= high; ++i) {
        *it = i;
        increment(first, next, last, remainder - i);
      }
    }
    
    int main()
    {
      std::vector<int> vect(3, 0);
      increment(vect.begin(), vect.begin(), vect.end(), sum);
    }
    This will generate all the combinations you need, in an efficient manner (it won't backtrack on "bad" combinations). It also outputs in a "natural" order. Unlike the previous algorithm though, it needs to retain state. So if you want to operate on the results, you need to either store them all for later use, or pass in a callback object on generated results.
    Last edited by monarch_dodra; March 1st, 2016 at 03:30 AM.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured