Arranjo, Combinação e Permutação
Autor: Eduardo Spaki | Publicado: 2011-09-08T18:51:00-03:00

  Essas três palavrinhas fazem parte de clássicos da computação e matemática. então resolvi explicá-las e demonstrar com a linguagem C.  Arranjo: são grupos sem repetição, ou seja, se eu tenho os elementos 1, 2 e 3, eu posso agrupá-los em dupla (conjuntos de dois elementos) para formar os seguintes números: 12, 13, 21, 23, 31, 32.  O arranjo é a base da Combinação e da permutação. Sua fórmula é descrita assim:     A n,p = n!/(n-p)!       Onde “n” é o quantidade de elementos e “p” o tamanho do grupo.  No exemplo mencionado temos 3 elementos (quantidade) colocados em par (tamanho do grupo = 2).     A 3,2 = 3!/(3-2)! = 3.2.1/1! = 6   Agora que já entendemos a base, vamos para o próximo, a Combinação.   Combinação: é um Arranjo onde deveremos ignoramos os grupos que contenha os mesmo elementos, independente da ordem.   No exemplo do Arranjo, nós consideramos conjuntos distintos os números 12 e 21, porque numericamente são diferentes.   Mas imagine o caso onde temos um João e uma Maria. Tanto faz se o conjunto é (João, Maria) ou (Maria, João), pois formam o mesmo casamento . Então essa é a Combinação: Arranjos que desconsideramos demais conjuntos que contenham o mesmo elemento independente da ordem.     C n,p = A n,p/p! = n!/p!(n-p)!     Percebam que é possível compor a fórmula da Combinação se baseando na fórmula do Arranjo.  Vamos ver um exemplo: Qual a quantidade de duplas possíveis com as pessoas Adriana, Xenofreuda e Tonha.     C 3,2 = 3!/2!(3-2)! = 3.2.1/2.1.1! = 3   E finalmente a Permutação.   Permutação: Arranjos onde a quantidade de elementos é igual ao tamanho do grupo, utilizado muito em anagramas. Anagramas são as variações possíveis de se fazer com as letras de uma palavra, por exemplo a palavra “mel”: mle, lme, lem, eml, elm.  Sua formula é um simples fatorial:     P n = n!     Para o exemplo:     P 3 = 3! = 3.2.1 = 6   Uma vez que entendemos a teoria, vamos colocá-la na pratica dentro da computação com a linguagem C.   Para isso, usei conceitos de Recursividade e Números Fatoriais.     // -&gt; Para poder usar o &quot;wcout&quot; e o cout     #include&lt;iostream&gt;      using namespace std;    long double CalcularFatorial(int numero)     {      &#160;&#160;&#160; if(numero &lt; 2)      &#160;&#160;&#160;&#160;&#160;&#160;&#160; return numero;    &#160;&#160;&#160; return numero * CalcularFatorial(numero - 1);     }    long double CalcularArranjo(int quantidadeElementos, int tamanhoGrupo)     {      &#160;&#160;&#160; return CalcularFatorial(quantidadeElementos)/CalcularFatorial(quantidadeElementos - tamanhoGrupo);      }    long double CalcularCombinacao(int quantidadeElementos, int tamanhoGrupo)     {      &#160;&#160;&#160; return CalcularArranjo(quantidadeElementos, tamanhoGrupo)/CalcularFatorial(tamanhoGrupo);      }    long double CalcularPermutacao(int quantidadeElementos)     {      &#160;&#160;&#160; return CalcularFatorial(quantidadeElementos);      }    void main()     {      &#160;&#160;&#160; setlocale(LC_ALL, &quot;Portuguese&quot;); // -&gt; Permitir acentos.    &#160;&#160;&#160; while(true)     &#160;&#160;&#160; {      &#160;&#160;&#160;&#160;&#160;&#160;&#160; // -&gt; Limpa a tela.      &#160;&#160;&#160;&#160;&#160;&#160;&#160; system(&quot;cls&quot;);    &#160;&#160;&#160;&#160;&#160;&#160;&#160; // -&gt; Monta o menu de opções.     &#160;&#160;&#160;&#160;&#160;&#160;&#160; wcout &lt;&lt; &quot;Qual operação deseja realizar?&quot; &lt;&lt; endl;      &#160;&#160;&#160;&#160;&#160;&#160;&#160; wcout &lt;&lt; &quot;\t1-) Fatorial&quot; &lt;&lt; endl;      &#160;&#160;&#160;&#160;&#160;&#160;&#160; wcout &lt;&lt; &quot;\t2-) Arranjo&quot; &lt;&lt; endl;      &#160;&#160;&#160;&#160;&#160;&#160;&#160; wcout &lt;&lt; &quot;\t3-) Combinação&quot; &lt;&lt; endl;      &#160;&#160;&#160;&#160;&#160;&#160;&#160; wcout &lt;&lt; &quot;\t4-) Permutação&quot; &lt;&lt; endl;      &#160;&#160;&#160;&#160;&#160;&#160;&#160; wcout &lt;&lt; &quot;\t5-) Sair&quot; &lt;&lt; endl;      &#160;&#160;&#160;&#160;&#160;&#160;&#160; wcout &lt;&lt; &quot;\tOpção: &quot;;    &#160;&#160;&#160;&#160;&#160;&#160;&#160; int opcao, quantidadeElementos, tamanhoGrupo;     &#160;&#160;&#160;&#160;&#160;&#160;&#160; long double resultado;    &#160;&#160;&#160;&#160;&#160;&#160;&#160; scanf(&quot;%i&quot;, &amp;opcao);    &#160;&#160;&#160;&#160;&#160;&#160;&#160; switch (opcao)     &#160;&#160;&#160;&#160;&#160;&#160;&#160; {      &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; case 1:      &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; wcout &lt;&lt; &quot;Infome um número para calcular seu fatorial: &quot;;      &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; scanf(&quot;%i&quot;, &amp;quantidadeElementos);    &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; resultado = CalcularFatorial(quantidadeElementos);     &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; break;      &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; case 2:      &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; wcout &lt;&lt; &quot;Cálculo de Arranjo&quot; &lt;&lt; endl;      &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; wcout &lt;&lt; &quot;Infome a quantidade de elementos: &quot;;      &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; scanf(&quot;%i&quot;, &amp;quantidadeElementos);      &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; wcout &lt;&lt; &quot;Infome o tamanho do grupo: &quot;;      &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; scanf(&quot;%i&quot;, &amp;tamanhoGrupo);    &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; resultado = CalcularArranjo(quantidadeElementos, tamanhoGrupo);     &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; break;      &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; case 3:      &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; wcout &lt;&lt; &quot;Cálculo de Combinação&quot; &lt;&lt; endl;      &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; wcout &lt;&lt; &quot;Infome a quantidade de elementos: &quot;;      &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; scanf(&quot;%i&quot;, &amp;quantidadeElementos);      &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; wcout &lt;&lt; &quot;Infome o tamanho do grupo: &quot;;      &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; scanf(&quot;%i&quot;, &amp;tamanhoGrupo);    &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; resultado = CalcularCombinacao(quantidadeElementos, tamanhoGrupo);     &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; break;      &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; case 4:      &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; wcout &lt;&lt; &quot;Cálculo de Permutação&quot; &lt;&lt; endl;      &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; wcout &lt;&lt; &quot;Infome a quantidade de elementos: &quot;;      &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; scanf(&quot;%i&quot;, &amp;quantidadeElementos);    &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; resultado = CalcularPermutacao(quantidadeElementos);     &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; break;      &#160;&#160;&#160;&#160;&#160;&#160;&#160; }    &#160;&#160;&#160;&#160;&#160;&#160;&#160; if(opcao &gt; 4) // -&gt; Encerra a aplicação     &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; break;    &#160;&#160;&#160;&#160;&#160;&#160;&#160; wcout &lt;&lt; endl &lt;&lt; &quot;Resultado: &quot; &lt;&lt; resultado &lt;&lt; endl &lt;&lt; endl;     &#160;&#160;&#160;&#160;&#160;&#160;&#160; wcout &lt;&lt; &quot;Pressione enter para continuar...&quot;;      &#160;&#160;&#160;&#160;&#160;&#160;&#160; fflush(stdin);      &#160;&#160;&#160;&#160;&#160;&#160;&#160; getchar();      &#160;&#160;&#160; }    &#160;&#160;&#160; wcout &lt;&lt; endl &lt;&lt; &quot;Fim!&quot;;     &#160;&#160;&#160; /*fflush(stdin);      &#160;&#160;&#160; getchar();*/      }   Percebam que, como vimos na teoria, é possível aproveitar o Arranjo para compor a formula da Combinação, e fiz exatamente isso na função CalcularCombinacao().   Outro ponto é que, para os resultados eu tive que apelar para o tipo long double (96 bits), pois quando falamos em fatoriais, os números podem crescer freneticamente.  Acompanhe alguns testes:        É isso daí pessoal.  Já calcularam qual o números de combinações possíveis de maquiagem de maquiagem da sua namorada, mulher ou enrolada?   Você irá se surpreender O.o