quarta-feira, 28 de novembro de 2012

Geração de combinações entre n elementos

comb3.h

#ifndef COMB3_H
#define COMB3_H

class comb3 {
public:
    comb3();
    comb3(const comb3& orig);
    virtual ~comb3();
    void elementos3();
private:

};

#endif /* COMB3_H */

comb3.cpp

#include 
using std::cout;
using std::endl;
#include 
#include "comb3.h"

comb3::comb3() {
}

comb3::comb3(const comb3& orig) {
}

comb3::~comb3() {
}

void comb3::elementos3()
{
  int p1=1, p2=1, p3=1;
  int total, totalc=0;

  total=pow(3,3);

  for (int i = 1; i <= total; i++)
  {
        if (p1 != p2 && p1 != p3 && p2 != p3)
        {
           totalc++;
           cout << p1 << " " << p2 << " " << p3 << endl;
        }

        if (p3 < 3)
        {
            p3++;
        }
        else
        {
           p2++;
           p3 = 1;
        }

        if (p2 == 4)
        {
           p2 = 1;
           p1++;
        }
  }
  cout << totalc << " combinações" << endl;    
}

comb4.h

#ifndef COMB4_H
#define COMB4_H

class comb4 {
public:
    comb4();
    comb4(const comb4& orig);
    virtual ~comb4();
    void elementos4();
private:

};

#endif /* COMB4_H */

comb4.cpp

#include 
using std::cout;
using std::endl;
#include 
#include "comb4.h"

comb4::comb4() {
}

comb4::comb4(const comb4& orig) {
}

comb4::~comb4() {
}

void comb4::elementos4()
{
    int p1 =1, p2 = 1, p3 = 1, p4 = 1;
    int total = pow(4,4);
    int totalc = 0;

    for (int i = 1; i <= total; i++) {
            if (p1 != p2 && p1 != p3 && p1 != p4 && p2 != p3  && p2 != p4 && p3 != p4) 
            {
               totalc++;
               cout << p1 << " " << p2 << " " << p3 << " " << p4 << endl;
            }

            if (p4 < 4) 
            {
               p4++;
            }
            else
            {
               p3++;
               p4 = 1;
            }

            if (p3 == 5) 
            {
               p3 = 1;
               p2++;
            }

            if (p2 == 5) 
            {
               p2 = 1;
               p1++;
            }
    }
    cout << totalc << " combinações" << endl;    
}

comb5.h

#ifndef COMB5_H
#define COMB5_H

class comb5 {
public:
    comb5();
    comb5(const comb5& orig);
    virtual ~comb5();
    void elementos5();
private:

};
#endif /* COMB5_H */

comb5.cpp

#include 
using std::cout;
using std::endl;
#include 
#include "comb5.h"

comb5::comb5() {
}

comb5::comb5(const comb5& orig) {
}

comb5::~comb5() {
}

void comb5::elementos5()
{
   int p1 = 1, p2 = 1, p3 = 1, p4 = 1, p5 = 1;
   int total = pow(5,5);
   int totalc = 0;

   for (int i = 1; i <= total; i++) 
   {
      if (p1 != p2 && p1 != p3 && p1 != p4 && p1 != p5 && p2 != p3 && p2 != p4 && 
p2 != p5 && p3 != p4 && p3 != p5 && p4 != p5) 
      {
         totalc++;
         cout << p1 << " " << p2 << " " << p3 << " " << p4 << " " << p5 << endl;
      }

      if (p5 < 5) 
      {
         p5++;
      }
      else
      {
        p4++;
        p5 = 1;
      }

      if (p4 == 6) 
      {
        p4 = 1;
        p3++;
      }

      if (p3 == 6) 
      {
        p3 = 1;
        p2++;
      }

      if (p2 == 6) 
      {
        p2 = 1;
        p1++;
      }
      
  }
  cout << totalc << " combinações" << endl;     
}

comb6.h

#ifndef COMB6_H
#define COMB6_H

class comb6 {
public:
    comb6();
    comb6(const comb6& orig);
    virtual ~comb6();
    void elementos6();
private:

};

#endif /* COMB6_H */

comb6.cpp

#include 
using std::cout;
using std::endl;
#include 
#include "comb6.h"

comb6::comb6() {
}

comb6::comb6(const comb6& orig) {
}

comb6::~comb6() {
}

void comb6::elementos6()
{
    int p1 = 1, p2 = 1, p3 = 1, p4 = 1, p5 = 1, p6 = 1;
    int total = pow(6, 6);
    int totalc = 0;

    for (int i = 1; i <= total; i++) 
    {
        if (p1 != p2 && p1 != p3 && p1 != p4 && p1 != p5 && p1 != p6 && p2 != p3 &&
 p2 != p4 && p2 != p5 && p2 != p6 && p3 != p4 && p3 != p5 && p3 != p6 && 
p4 != p5 && p4 != p6 && p5 != p6) 
        {
           totalc++;
           cout <<  p1 << " " << p2 << " " << p3 << " " << p4 << " " << p5 << " " 
<< p6 << endl;
        }

        if (p6 < 6) 
          {
            p6++;
          }
        else
        {
            p5++;
            p6 = 1;
        }

        if (p5 == 7) 
        {
           p5 = 1;
           p4++;
        }

        if (p4 == 7) 
        {
           p4 = 1;
           p3++;
        }

        if (p3 == 7) 
        {
           p3 = 1;
           p2++;
        }

        if (p2 == 7) 
        {
           p2 = 1;
           p1++;
        }                                                                               
    }
    cout << totalc << " combinações" << endl;    
}
  • Shell Script
  • PHP
  • C++
Elementos Combinações Shell Script PHP C++
3 6 0m0.016s 0m0.00069s 0m0.005s
4 24 0m0.051s 0m0.0012s 0m0.005s
5 120 0m0.471s 0m0.015s 0m0.008s
6 720 0m9.639s 0m0.25s 0m0.023s
7 5040 5m44.219s 0m4.98s 0m0.159s
8 40320 142m51.553s 1m52s 0m1.712s
9 362880 - - 0m24.090s