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 <
#include <
#include <
const std::vector<std::string> &str) {
▲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)
} ▼
void combination_with_repetiton(int sabores, int bolas, const vector<string>& v_sabores){ ▼
vector<int> v(bolas+1, 0); ▼
while (true){ ▼
v[i + 1] += 1; ▼
for (int k = i; k >= 0; --k){ ▼
}▼
}
// idea: custom number system with 2s complement like 0b10...0==MIN stop case
int main(){ ▼
vector<string> options{ "iced", "jam", "plain" }; ▼
combination_with_repetiton(3, 2, options); ▼
return 0; ▼
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
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
}
}
</lang>
Line 913 ⟶ 911:
{{out}}
<pre>
iced
jam
jam plain
plain
</pre>
|