从[0,NN-1]这NN个数里面,找到所有组合 例如Con(5,3)
0 1 2
0 1 3
0 1 4
0 2 3
0 2 4
0 3 4
1 2 3
1 2 4
1 3 4
2 3 4
类似于加法器,初始是0,1,2
每次给最后的一个数字加一
令Kmax=3,k在[0,Kmax]
则第k位置的数字进位的要求是
ve[k] >= N - (Kmax - k)
直到ve[0]>0,循环结束
#include <vector>
#include <iostream>
using namespace std;
using LL = long long int;
#define debug(x) cout<<#x<<": "<<x<<endl;
bool Con(LL NN, LL MM) {
LL Kmax = MM;
LL k = Kmax;
LL N = NN;
vector<LL> ve(Kmax + 1, 0);
for (LL i = 1; i <= Kmax; i++) {
ve[i] = i - 1;
}
do {
for (LL i = 1; i <= Kmax; i++) {
cout << ve[i] << " ";
}
cout << endl;
ve[k]++;
if (ve[k] >= N) {
while (k >= 1 && ve[k] >= N - (Kmax - k)) {
k--;
ve[k]++;
}
k++;
while (k <= Kmax) {
ve[k] = ve[k - 1] + 1;
k++;
}
k = Kmax;
}
} while (ve[0] == 0);
return true;
}
int main()
{
Con(5,3);
return 0;
}