Combinations with repetitions: Difference between revisions

replace algorithm with format that prints expected ascending order
(Combinations with repetitions en BASIC256)
(replace algorithm with format that prints expected ascending order)
Line 871:
Non recursive version.
<lang cpp>
#include <cstdioiostream>
#include <vectorstring>
#include <stringvector>
 
void print_vector(const std::vector<int> &vpos, size_t n, const vector<string> &s){
using namespace std;
const std::vector<std::string> &str) {
for (size_t i = 01; i < npos.size(); ++i) // offset: i:1..N
void print_vector(const vector<int> &v, size_t n, const vector<string> &s){
printf("%s\t", str[pos[i]].c_str()); // str: 0..N-1
for (size_t i = 0; i < n; ++i)
printf("%s\tn", s[v[i]].c_str());
printf("\n");
}
void combination_with_repetiton(int sabores, int bolas, const vector<string>& v_sabores){
sabores--;
vector<int> v(bolas+1, 0);
while (true){
for (int i = 0; i < bolas; ++i){ //vai um
if (v[i] > sabores){
v[i + 1] += 1;
for (int k = i; k >= 0; --k){
v[k] = v[i + 1];
}
//v[i] = v[i + 1];
}
}
if (v[bolas] > 0)
break;
print_vector(v, bolas, v_sabores);
v[0] += 1;
}
}
 
// idea: custom number system with 2s complement like 0b10...0==MIN stop case
int main(){
void combination_with_repetiton(int saboresn, int bolask, const vector<string>& v_sabores){
vector<string> options{ "iced", "jam", "plain" };
v[iconst +std::vector<std::string> 1] += 1;&str) {
combination_with_repetiton(3, 2, options);
std::vector<int> vpos(bolask + 1, 0);
return 0;
while (true) {
for (int i = k; i > 0; i -= 1) {
if (pos[i] > n - 1) // if number spilled over: xx0(n-1)xx
{
pos[i - 1] += 1; // set xx1(n-1)xx
for (int kj = i; kj ><= 0k; --kj += 1){
pos[j] = pos[j - 1]; // set xx11..1
}
}
if (pos[0] > 0) // stop condition: 1xxxx
break;
print_vector(pos, str);
pos[k] += 1; // xxxxN -> xxxxN+1
}
}
 
int main(){ {
std::vector<std::string> optionsstr{ "iced", "jam", "plain" };
combination_with_repetiton(3, 2, optionsstr);
return 0;
}
</lang>
Line 913 ⟶ 911:
{{out}}
<pre>
iced iced
jam iced jam
plain iced plain
jam jam
jam plain
plain jam
plain plain
</pre>
 
37

edits