C ++ Newbie precisa de ajuda para imprimir combinações de inteiros
Suponha que eu seja dado:
Um intervalo de inteirosiRange
(isto é, de1
atéiRange
) eUm número desejado de combinaçõesEu quero encontrar o número de todas as combinações possíveis e imprimir todas essas combinações.
Por exemplo:
Dado: iRange = 5
en = 3
Então o número de combinações éiRange! / ((iRange!-n!)*n!) = 5! / (5-3)! * 3! = 10
combinações e a saída é:
123 - 124 - 125 - 134 - 135 - 145 - 234 - 235 - 245 - 345
Outro exemplo:
Dado: iRange = 4
en = 2
Então o número de combinações éiRange! / ((iRange!-n!)*n!) = 4! / (4-2)! * 2! = 6
combinações e a saída é:
12 - 13 - 14 - 23 - 24 - 34
Minha tentativa até agora é:
#include <iostream>
using namespace std;
int iRange= 0;
int iN=0;
int fact(int n)
{
if ( n<1)
return 1;
else
return fact(n-1)*n;
}
void print_combinations(int n, int iMxM)
{
int iBigSetFact=fact(iMxM);
int iDiffFact=fact(iMxM-n);
int iSmallSetFact=fact(n);
int iNoTotComb = (iBigSetFact/(iDiffFact*iSmallSetFact));
cout<<"The number of possible combinations is: "<<iNoTotComb<<endl;
cout<<" and these combinations are the following: "<<endl;
int i, j, k;
for (i = 0; i < iMxM - 1; i++)
{
for (j = i + 1; j < iMxM ; j++)
{
//for (k = j + 1; k < iMxM; k++)
cout<<i+1<<j+1<<endl;
}
}
}
int main()
{
cout<<"Please give the range (max) within which the combinations are to be found: "<<endl;
cin>>iRange;
cout<<"Please give the desired number of combinations: "<<endl;
cin>>iN;
print_combinations(iN,iRange);
return 0;
}
Meu problema: A parte do meu código relacionada à impressão das combinações funciona apenas paran = 2, iRange = 4
e eu não posso fazer isso funcionar em geral, ou seja, para qualquern
eiRange
.