问题描述
假设我得到:
- 整数范围
iRange
从1
最多到iRange
)和 - / li>
我想查找所有可能组合的数目并打印出所有这些组合。
例如:
给定: iRange = 5
和 n = 3
然后组合的数量是 iRange! /((iRange!-n!)* n!)= 5! /(5-3)! * 3! = 10
组合,输出为:
123 - 124 - 125 - 134 - 145 - 234 - 235 - 245 - 345
另一个例子:
给定: iRange = 4
和 n = 2
p>
那么组合的数量是 iRange! /((iRange!-n!)* n!)= 4! /(4-2)! * 2! = 6
组合,输出为:
12 - 13 - 14 - 23 - 24 - 34
我到目前为止的尝试是:
#include< iostream>
using namespace std;
int iRange = 0;
int iN = 0;
int fact(int n)
{
if(n 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<<可能的组合的数量是: cout<<,这些组合如下:<< endl;
int i,j,k;
for(i = 0; i {
for(j = i + 1; j {
// for(k = j + 1; k cout<< i + 1<< j + 1&
}
}
}
int main()
{
cout<<请提供范围将找到组合:<<< endl;
cin>> iRange;
cout<<请给出所需的组合数:<< endl;
cin>> iN;
print_combinations(iN,iRange);
return 0;
}
我的问题:
的我的代码相关的打印的组合只适用于 n = 2,iRange = 4
,我不能让它一般工作,即任何 n
和 iRange
。
以下是您的代码已修改:D:D使用递归解决方案:
#include < iostream>
int iRange = 0;
int iN = 0; //从iRange获取的项目数,对于这些项目,我想打印出组合
int iTotalCombs = 0;
int * pTheRange;
int * pTempRange;
int find_factorial(int n)
{
if(n return 1;
else
return find_factorial(n-1)* n;
}
// --->这里是另一个解决方案:
void print_out_combinations(int * P,int K,int n_i)
{
if(K == 0)
{
for(int j = iN; j> 0; j--)
std :: cout<< P [j] ;;
std :: cout<< std :: endl;
}
else
for(int i = n_i; i {
P [K] = pTheRange [i]
print_out_combinations(P,K-1,i + 1);
}
}
//这里结束了解决方案...
int main()
{
std :: cout< ;给出项目集-iRange- =;
std :: cin>> iRange;
std :: cout<<给出要创建组合的iRange的项目#-iN- =;
std :: cin>> iN;
pTheRange = new int [iRange];
for(int i = 0; i< iRange; i ++)
{
pTheRange [i] = i + 1;
}
pTempRange = new int [iN];
iTotalCombs =(find_factorial(iRange)/(find_factorial(iRange-iN)* find_factorial(iN)));
std :: cout<<可能的组合数是:< std :: cout<<因为从一组大小< iRange<<中绘制的 print_out_combinations(pTempRange,iN,0);
return 0;
}
Suppose I am given:
- A range of integers
iRange
(i.e. from1
up toiRange
) and - A desired number of combinations
I want to find the number of all possible combinations and print out all these combinations.
For example:
Given: iRange = 5
and n = 3
Then the number of combinations is iRange! / ((iRange!-n!)*n!) = 5! / (5-3)! * 3! = 10
combinations, and the output is:
123 - 124 - 125 - 134 - 135 - 145 - 234 - 235 - 245 - 345
Another example:
Given: iRange = 4
and n = 2
Then the number of combinations is iRange! / ((iRange!-n!)*n!) = 4! / (4-2)! * 2! = 6
combinations, and the output is:
12 - 13 - 14 - 23 - 24 - 34
My attempt so far is:
#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;
}
My problem:The part of my code related to the printing of the combinations works only for n = 2, iRange = 4
and I can't make it work in general, i.e., for any n
and iRange
.
Here is your code edited :D :D with a recursive solution:
#include <iostream>
int iRange=0;
int iN=0; //Number of items taken from iRange, for which u want to print out the combinations
int iTotalCombs=0;
int* pTheRange;
int* pTempRange;
int find_factorial(int n)
{
if ( n<1)
return 1;
else
return find_factorial(n-1)*n;
}
//--->Here is another solution:
void print_out_combinations(int *P, int K, int n_i)
{
if (K == 0)
{
for (int j =iN;j>0;j--)
std::cout<<P[j]<<" ";
std::cout<<std::endl;
}
else
for (int i = n_i; i < iRange; i++)
{
P[K] = pTheRange[i];
print_out_combinations(P, K-1, i+1);
}
}
//Here ends the solution...
int main()
{
std::cout<<"Give the set of items -iRange- = ";
std::cin>>iRange;
std::cout<<"Give the items # -iN- of iRange for which the combinations will be created = ";
std::cin>>iN;
pTheRange = new int[iRange];
for (int i = 0;i<iRange;i++)
{
pTheRange[i]=i+1;
}
pTempRange = new int[iN];
iTotalCombs = (find_factorial(iRange)/(find_factorial(iRange-iN)*find_factorial(iN)));
std::cout<<"The number of possible combinations is: "<<iTotalCombs<<std::endl;
std::cout<<"i.e.the combinations of "<<iN<<" elements drawn from a set of size "<<iRange<<" are: "<<std::endl;
print_out_combinations(pTempRange, iN, 0);
return 0;
}
这篇关于C ++ Newbie需要帮助打印整数的组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!