Need help, loops...
Hi,
I am trying to write a program to count dices. The program takes a
single integer 'n' (number of dice), it then outputs between 'n' and
'6*n', what combinations of the n dice result sums to it. Say, n=4,
sum=5, then it outputs 1 1 1 2 5, 1 1 2 1 5, 1 2 1 1 5, 2 1 1 1 5. I am
stuck with a problem, since my program is supposed to accept arbitrary
number of dice, I can't come up with a way to code the loops. Remember
it needs to iterate through arbitrary number of dices. Here is what I
have (compilies/runs/result is wrong for obvious reasons)
Let me know if you can come up with a smart pattern to solve this:
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <iterator>
using namespace std;
int main(void){
unsigned int n, i,k;
while(cin){
cin >> n;
string line;
getline(cin, line);
unsigned int min=n, max=n*6, sum=0;
vector<unsigned int> dice(n);
for(i=min; i <= max; i ++){
for(k=0; k < n; k ++)
for(j=1; j <= 6; j ++)
dice[k] = j;
sum = accumulate(dice.begin(), dice.end(), 0);
if(sum == i) {
copy(dice.begin(), dice.end(),
ostream_iterator<unsigned int>(cout, " "));
cout << i << ' ' << endl;
}
}
}
return 0;
}