RosettaCodeData/Task/Combinations-with-repetitions/C++/combinations-with-repetitions.cpp

39 lines
1.1 KiB
C++
Raw Permalink Normal View History

2019-09-12 10:33:56 -07:00
#include <cstdio>
2017-09-23 10:01:46 +02:00
#include <vector>
#include <string>
using namespace std;
2019-09-12 10:33:56 -07:00
void print_vector(const vector<int> &v, size_t n, const vector<string> &s){
for (size_t i = 0; i < n; ++i)
2017-09-23 10:01:46 +02:00
printf("%s\t", s[v[i]].c_str());
printf("\n");
}
2019-09-12 10:33:56 -07:00
void combination_with_repetiton(int sabores, int bolas, const vector<string>& v_sabores){
2017-09-23 10:01:46 +02:00
sabores--;
2019-09-12 10:33:56 -07:00
vector<int> v(bolas+1, 0);
2017-09-23 10:01:46 +02:00
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;
}
}
int main(){
2019-09-12 10:33:56 -07:00
vector<string> options{ "iced", "jam", "plain" };
2017-09-23 10:01:46 +02:00
combination_with_repetiton(3, 2, options);
return 0;
}