C++ 組み合わせ列挙
#include <vector> using namespace std; vector<int> next_comb(int n, int k, vector<int> v){ for(int i = k-1; i >= 0; --i){ if (v[i] != i+n-k){ int t = ++v[i]; for (int j = i+1; j < k; ++j){ v[j] = ++t; } return v; } } for (int i = 0; i < k; ++i){ v[i] = i; } return v; } vector<int> v = {0, 1, 2}; for (int i = 0; i < 10; ++i){ for (int j = 0; j < 3; ++j){ cout << v[j]; } cout << endl; v = next_comb(5, 3, v); } /* 012 013 014 023 ... 234 */