33,008
社区成员
发帖
与我相关
我的任务
分享
//从左到右扫描数组元素值的“10”组合
j = 0;
while(subset[j + 1] <= subset[j] + 1)
j++;
//找到第一个“10”组合后将其变为 “01”组合
subset[j]++;
//将其左边的所有“1”全部移动到数组的最左端
for(i = 0; i < j; i++)
subset[i] = i;
return 1;
//2楼的
j=m;
while( B[j]==n )
--j;
++B[j];
for( i=j+1; i<=m; ++i )
B[i]=B[j];
swap( A[j], A[B[j]] );
//11楼的
j = 0;
while(subset[j + 1] <= subset[j] + 1)
j++;
subset[j]++;
for(i = 0; i < j; i++)
subset[i] = i;
return 1;
#include <windows.h>
#include <iostream>
#include <iterator>
using namespace std;
void combination( int n, int m ){
int *c=new int[m+2], j;
for( j=1; j<=m; ++j ) c[j]=j-1;
c[m+1]=n;
R2: ;//copy( c+1, c+m+1, ostream_iterator<int>(cout," ") ),cout<<endl;
if( m&1 )
if( c[1]+1<c[2] ){
++c[1]; goto R2;
}
else{
j=2; goto R4;
}
else
if( c[1]>0 ){
--c[1]; goto R2;
}
else{
j=2; goto R5;
};
R4: if( c[j]>=j ){
c[j]=c[j-1]; c[j-1]=j-2; goto R2;
}
else
++j;
R5: if( c[j]+1<c[j+1] ){
c[j-1]=c[j]; c[j]=c[j]+1; goto R2;
}
else{
++j;
if( j<=m ) goto R4;
}
delete []c;
}
int main(int argc, char *argv[])
{
int time=GetTickCount();
combination( 23, 11 );
cout<<GetTickCount()-time<<" ms\n";
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
template <class T>
inline void PRINT_ELEMENTS (const T& coll, char tokens[], const char* optcstr="")
{
typename T::const_iterator pos;
std::cout << optcstr;
for (pos=coll.begin(); pos!=--coll.end(); ++pos) {
std::cout << tokens[*pos] << ' ';
}
std::cout << std::endl;
}
//计算组合数C(n,m)
int C(int n, int m)
{
double result = 1;
for (int i = 0; i < m; i++)
{
result *= n - i;
result /= i + 1;
}
return (int)result;
}
//计算第rank个组合,rank从0到C(n,card)-1
void unrankCombination( int rank, vector<int> &subset, int card)
{
int p, m;
m = rank;
for(int i = card - 1; i >= 0; i--) {
p = i;
while( C(p+1, i+1) <= m ) p++;
m -= C( p, i+1 );
subset[i] = p;
}
}
int main(int argc, char* argv[])
{
int m = 10;
int n = 15;
vector<int> combination( m + 1 );
char tokens[] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O'};
for( int rank = 0; rank < C( n, m ); rank++)
{
unrankCombination( rank, combination, m );
cout << rank + 1 << ": ";
PRINT_ELEMENTS( combination, tokens );
}
cout << "\n\tpress Enter to exit ..." << endl;
cin.get();
return 0;
}
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
#define maxv 100
void getFirstCombination(vector<int> &subset, int card)
{
int i;
for(i = 0; i < card; i++)
subset[i] = i;
subset[i] = maxv + 1;
}
int getNextCombination(vector<int> &subset, int card, int v)
{
int i,j;
if(subset[0] >= v - card)
return 0;
else {
j = 0;
while(subset[j + 1] <= subset[j] + 1)
j++;
subset[j]++;
for(i = 0; i < j; i++)
subset[i] = i;
return 1;
}
}
template <class T>
inline void PRINT_ELEMENTS (const T& coll, char tokens[], const char* optcstr="")
{
typename T::const_iterator pos;
std::cout << optcstr;
for (pos=coll.begin(); pos!=--coll.end(); ++pos) {
std::cout << tokens[*pos] << ' ';
}
std::cout << std::endl;
}
int main(int argc, int argv[])
{
int card = 5;
int vol = 10;
int index = 1;
vector<int> combination(card+1);
char tokens[] = {'A','B','C','D','E','F','G','H','I','J'};
getFirstCombination( combination, card );
do{
cout << index << ": ";
PRINT_ELEMENTS( combination, tokens );
index++;
}while( getNextCombination(combination, card, vol) );
cout << "\n\tpress Enter to exit ..." << endl;
cin.get();
return 0;
}