Click here to Skip to main content
15,897,519 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#include <iostream>
#include <vector>
using namespace std;
vector<int> vec;//for counting total number of ways.
int sum(int m,int n,int total);//function prototype

int main()
{
    int n,m,total;
    cout<<"Enter the values of n,m,total respectively"<<endl;
    cin>>n>>m>>total;
    sum(m,n,total);
    cout<<vec.size()<<endl;
    return 0;
}
int sum(int m,int n,int total){

for(int i=1;i<=m;i++){
         if(n>=0&&total-i==0)
                 vec.push_back(0);//size of vector increases with each pushback,and no 
                                  //no of ways =vec.size()
                         
             if(n<0)
                    return 0;
               else
                     return  sum(m,n-1,total-i);

}

}


What I have tried:

i ve tried looking for solution for similar problem,but couldn't find any,any help is appreciated.!!
Posted
Updated 1-Aug-18 1:42am
v3
Comments
Patrice T 31-Jul-18 12:27pm    
Show sample input, expected output and your actual output.
given more explanations.
Richard MacCutchan 1-Aug-18 4:00am    
You first need to figure out the algorithm to solve your question. Only once you know that should you start to write any code.

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think! Start by researching "combinations and permutations" and see what algorithms you can find.

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
 
Share this answer
 
A brutal approach (there are better ways) would be counting from 0 to (m^n-1). At each iteration the counter represents a valid dice configuration and you can check if the sum of dices makes up total.
E.g.
Suppose you have two 6-ways dices (i.e. n=2, m=6) and total = 5.
Then you have to iterate from 0 to 6^2-1=35
counter  dice-1  dice-0  sum
  0        1        1     2
  1        1        2     3
  2        1        3     4
  3        1        4     5  OK
  4        1        5     6
  5        1        6     7
  6        2        1     3
  7        2        2     4
  8        2        3     5  OK
  9        2        4     6 
 10        2        5     7
 11        2        6     8
 ...........................
 ...........................
 35        6        6    12
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900