I'm including the revised version, as far as I'm concerned this looks good. Any opinions on calling the helper function from the parameter of the while loop?
Revised:
#include <iostream>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <vector>
int combination_modify(std::vector<int> &combination, int max_val);
int main()
{
int round = 0;
int n, k;
while (std::cin >> n >> k)
{
//init
std::vector<int> combination(k);
std::iota(combination.begin(), combination.end(), 1);
std::cout << "case " << ++round << ": " << std::endl;
while((combination_modify(combination, n)) >=0)
{
std::copy(combination.begin(), combination.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
}
std::cout << std::endl;
}
}
int combination_modify(std::vector<int> &combination, int max_val)
{
int pos = -1;
for (int i = combination.size() - 1; i >= 0; --i, --max_val)
{
if (combination[i] != max_val)
{
pos = i;
break;
}
}
if (pos < 0) {return pos;}
int val = combination[pos];
for (unsigned i = pos; i < combination.size(); ++i)
{
combination[i] = ++val;
}
return pos;
}
Original:
I'm including the revised version, as far as I'm concerned this looks good. Any opinions on calling the helper function from the parameter of the while loop?
Revised:
#include <iostream>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <vector>
int combination_modify(std::vector<int> &combination, int max_val);
int main()
{
int round = 0;
int n, k;
while (std::cin >> n >> k)
{
//init
std::vector<int> combination(k);
std::iota(combination.begin(), combination.end(), 1);
std::cout << "case " << ++round << ": " << std::endl;
while((combination_modify(combination, n)) >=0)
{
std::copy(combination.begin(), combination.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
}
std::cout << std::endl;
}
}
int combination_modify(std::vector<int> &combination, int max_val)
{
int pos = -1;
for (int i = combination.size() - 1; i >= 0; --i, --max_val)
{
if (combination[i] != max_val)
{
pos = i;
break;
}
}
if (pos < 0) {return pos;}
int val = combination[pos];
for (unsigned i = pos; i < combination.size(); ++i)
{
combination[i] = ++val;
}
return pos;
}
Original:
I'm including the revised version, as far as I'm concerned this looks good. Any opinions on calling the helper function from the parameter of the while loop?
Revised:
#include <iostream>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <vector>
int combination_modify(std::vector<int> &combination, int max_val);
int main()
{
int round = 0;
int n, k;
while (std::cin >> n >> k)
{
//init
std::vector<int> combination(k);
std::iota(combination.begin(), combination.end(), 1);
std::cout << "case " << ++round << ": " << std::endl;
while((combination_modify(combination, n)) >=0)
{
std::copy(combination.begin(), combination.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
}
std::cout << std::endl;
}
}
int combination_modify(std::vector<int> &combination, int max_val)
{
int pos = -1;
for (int i = combination.size() - 1; i >= 0; --i, --max_val)
{
if (combination[i] != max_val)
{
pos = i;
break;
}
}
if (pos < 0) {return pos;}
int val = combination[pos];
for (unsigned i = pos; i < combination.size(); ++i)
{
combination[i] = ++val;
}
return pos;
}
Original:
I'm including the revised version, as far as I'm concerned this looks good. Any opinions on calling the helper function from the parameter of the while loop?
Revised:
#include <iostream>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <vector>
int combination_modify(std::vector<int> &combination, int max_val);
int main()
{
int round = 0;
int n, k;
while (std::cin >> n >> k)
{
//init
std::vector<int> combination(k);
std::iota(combination.begin(), combination.end(), 1);
std::cout << "case " << ++round << ": " << std::endl;
while((combination_modify(combination, n)) >=0)
{
std::copy(combination.begin(), combination.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
}
std::cout << std::endl;
}
}
int combination_modify(std::vector<int> &combination, int max_val)
{
int pos = -1;
for (int i = combination.size() - 1; i >= 0; --i, --max_val)
{
if (combination[i] != max_val)
{
pos = i;
break;
}
}
if (pos < 0) {return pos;}
int val = combination[pos];
for (unsigned i = pos; i < combination.size(); ++i)
{
combination[i] = ++val;
}
return pos;
}
Original:
I already solved the problem, but I likeit seems my code is too awkward, and with a different arrpoachapproach there could be a mormore elegant solution. Notice
Notice that the general algorithm is described in the instructions:
This is the problem description:
Given unsigned integers 0 ≤ k ≤ n, generate all the k-combinations of the n objects, numbered 1, 2, ... , and n, using the following algorithm:
Starting with the combination 1, 2, ..., k, the next combination is found by scanning the current combination from right to left so as to locate the rightmost element that has not yet attained its maximum value. This element is incremented by one, and all positions to its right are reset to the lowest values possible.
Each line contains a set of n and k which separated with at least one space.This is the problem description:
For each case, the first line is a prompt as following: "case 1:". The next lines are all of the combinations. Notice that one line for one combination and list the results in ascending order. You don’t have to print any thing if the result is null. The last line of each case is an empty line, even if the result is null.
Given unsigned integers 0 ≤ k ≤ n, generate all the k-combinations of the n objects, numbered 1, 2, ... , and n, using the following algorithm:
Starting with the combination 1, 2, ..., k, the next combination is found by scanning the current combination from right to left so as to locate the rightmost element that has not yet attained its maximum value. This element is incremented by one, and all positions to its right are reset to the lowest values possible.
Each line contains a set of n and k which separated with at least one space.
For each case, the first line is a prompt as following: "case 1:". The next lines are all of the combinations. Notice that one line for one combination and list the results in ascending order. You don’t have to print any thing if the result is null. The last line of each case is an empty line, even if the result is null.
I already solved the problem, but I like my code is too awkward and with a different arrpoach there could be a mor elegant solution. Notice that the general algorithm is described in the instructions:
This is the problem description:
Given unsigned integers 0 ≤ k ≤ n, generate all the k-combinations of the n objects, numbered 1, 2, ... , and n, using the following algorithm:
Starting with the combination 1, 2, ..., k, the next combination is found by scanning the current combination from right to left so as to locate the rightmost element that has not yet attained its maximum value. This element is incremented by one, and all positions to its right are reset to the lowest values possible.
Each line contains a set of n and k which separated with at least one space.
For each case, the first line is a prompt as following: "case 1:". The next lines are all of the combinations. Notice that one line for one combination and list the results in ascending order. You don’t have to print any thing if the result is null. The last line of each case is an empty line, even if the result is null.
I already solved the problem, but it seems my code is too awkward, and with a different approach there could be a more elegant solution.
Notice that the general algorithm is described in the instructions:
This is the problem description:
Given unsigned integers 0 ≤ k ≤ n, generate all the k-combinations of the n objects, numbered 1, 2, ... , and n, using the following algorithm:
Starting with the combination 1, 2, ..., k, the next combination is found by scanning the current combination from right to left so as to locate the rightmost element that has not yet attained its maximum value. This element is incremented by one, and all positions to its right are reset to the lowest values possible.
Each line contains a set of n and k which separated with at least one space.
For each case, the first line is a prompt as following: "case 1:". The next lines are all of the combinations. Notice that one line for one combination and list the results in ascending order. You don’t have to print any thing if the result is null. The last line of each case is an empty line, even if the result is null.