Click here to Skip to main content
15,913,250 members
Home / Discussions / Algorithms
   

Algorithms

 
GeneralRe: Records and Clusters Pin
DQNOK6-Nov-08 4:54
professionalDQNOK6-Nov-08 4:54 
GeneralRe: Records and Clusters Pin
Member 41945936-Nov-08 7:57
Member 41945936-Nov-08 7:57 
GeneralRe: Records and Clusters Pin
Member 419459325-Apr-09 4:28
Member 419459325-Apr-09 4:28 
GeneralRe: Records and Clusters Pin
DQNOK27-Apr-09 3:13
professionalDQNOK27-Apr-09 3:13 
GeneralRe: Records and Clusters Pin
DQNOK27-Apr-09 5:57
professionalDQNOK27-Apr-09 5:57 
GeneralRe: Records and Clusters Pin
Member 419459327-Apr-09 11:22
Member 419459327-Apr-09 11:22 
QuestionRePosted from C# Forums: A Job Exam Question Pin
Bulky Fellow4-Nov-08 20:31
Bulky Fellow4-Nov-08 20:31 
AnswerRe: RePosted from C# Forums: A Job Exam Question Pin
Arash Partow4-Nov-08 21:59
Arash Partow4-Nov-08 21:59 
Took roughly ~4mins, the next_combination function (similar to next_permutation) may someday end-up in the STL, if so probably 2mins or so.... Smile | :)


#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>

template <class BidirectionalIterator>
bool next_combination(BidirectionalIterator first,
                      BidirectionalIterator k,
                      BidirectionalIterator last);

int main()
{

   std::vector<int> v;
   v.push_back(7);
   v.push_back(8);
   v.push_back(12);
   v.push_back(3);
   v.push_back(4);

   int target = 15;
   int combination_count = 0;

   std::sort(v.begin(),v.end());

   for(unsigned int i = 1; i <= v.size(); ++i)
   {
      std::vector<int> tmp;
      std::copy(v.begin(),v.end(),std::back_inserter(tmp));
      do
      {
         if (std::accumulate(tmp.begin(),tmp.begin() + i, 0) == target)
         {
            std::cout << ++combination_count << "] ";
            for(unsigned int j = 0; j < i; ++j)
            {
               std::cout << tmp[j] << (j < (i-1) ? "+" : "\n");
            }
         }
      }
      while(next_combination(tmp.begin(),tmp.begin() + i,tmp.end()));
   }

   if (0 == combination_count)
   {
      std::cout << "No combinations found." < std::endl;
   }
   return 0;
}


/*
   Credits: Mark Nelson http://marknelson.us/
*/
template <class BidirectionalIterator>
bool next_combination(BidirectionalIterator first,
                      BidirectionalIterator k,
                      BidirectionalIterator last)
{
   if ((first == last) || (k == first) || (k == last)) return false;
   BidirectionalIterator i = first;
   BidirectionalIterator ii = last;
   ++i;
   if (i == last) return false;
   i = last;
   --i;

   i = k;
   --ii;
   while (i != first)
   {
      if (*--i < *ii)
      { // Search down to find first comb entry less than final entry
         BidirectionalIterator j = k;
         while(!(*i < *j)) j++; // Find swap position [good-1-high|good-2-low]
         iter_swap(i,j); // Swap [good-2-high|good-1-low]
         i++;j++;ii=k; // Move to rotation positions
         rotate(i,j,last); // Rotate [good-2-low-high-good-1]
         while(j!=last){ ++j; ++ii;} // Find high good position
         rotate(k,ii,last); // Rotate [good-2-(low/high>|good-1-<low/high)]
         return true;
      }
   }
   rotate (first,k,last);
   return false;
}

AnswerRe: RePosted from C# Forums: A Job Exam Question Pin
Mark Churchill5-Nov-08 1:46
Mark Churchill5-Nov-08 1:46 
GeneralRe: RePosted from C# Forums: A Job Exam Question Pin
riced9-Nov-08 4:03
riced9-Nov-08 4:03 
GeneralRe: RePosted from C# Forums: A Job Exam Question Pin
Bulky Fellow9-Nov-08 7:45
Bulky Fellow9-Nov-08 7:45 
GeneralRe: RePosted from C# Forums: A Job Exam Question Pin
Mark Churchill9-Nov-08 11:33
Mark Churchill9-Nov-08 11:33 
QuestionTrammel method for constructing an ellipse Pin
hxhl9525-Oct-08 19:38
hxhl9525-Oct-08 19:38 
AnswerRe: Trammel method for constructing an ellipse Pin
73Zeppelin25-Oct-08 23:52
73Zeppelin25-Oct-08 23:52 
GeneralRe: Trammel method for constructing an ellipse [modified] Pin
Luc Pattyn26-Oct-08 9:02
sitebuilderLuc Pattyn26-Oct-08 9:02 
GeneralRe: Trammel method for constructing an ellipse Pin
hxhl9526-Oct-08 16:43
hxhl9526-Oct-08 16:43 
QuestionPRECISION and ACCURACY Pin
εїзεїзεїз22-Oct-08 1:27
εїзεїзεїз22-Oct-08 1:27 
JokeRe: PRECISION and ACCURACY Pin
CPallini22-Oct-08 2:21
mveCPallini22-Oct-08 2:21 
AnswerRe: PRECISION and ACCURACY Pin
73Zeppelin22-Oct-08 2:58
73Zeppelin22-Oct-08 2:58 
GeneralRe: PRECISION and ACCURACY Pin
εїзεїзεїз22-Oct-08 3:10
εїзεїзεїз22-Oct-08 3:10 
GeneralRe: PRECISION and ACCURACY Pin
Andrew Rissing22-Oct-08 6:53
Andrew Rissing22-Oct-08 6:53 
GeneralRe: PRECISION and ACCURACY Pin
73Zeppelin22-Oct-08 7:02
73Zeppelin22-Oct-08 7:02 
GeneralRe: PRECISION and ACCURACY Pin
Paul Conrad22-Oct-08 8:05
professionalPaul Conrad22-Oct-08 8:05 
GeneralRe: PRECISION and ACCURACY Pin
PIEBALDconsult13-Nov-08 16:14
mvePIEBALDconsult13-Nov-08 16:14 
AnswerRe: PRECISION and ACCURACY Pin
Roger Wright23-Oct-08 11:28
professionalRoger Wright23-Oct-08 11:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.