当m掷骰子被投掷时,你无法获得总和'总数' [英] No of ways you can get a sum 'total' when n dice with m ways are thrown

查看:117
本文介绍了当m掷骰子被投掷时,你无法获得总和'总数'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#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);

}

}





我的尝试:



i尝试寻找类似问题的解决方案,但找不到,任何帮助都表示赞赏。!!



What I have tried:

i ve tried looking for solution for similar problem,but couldn't find any,any help is appreciated.!!

推荐答案

我们不做你的功课:这是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



亲自尝试,你可能会发现它不是像你想的那么难!首先研究组合和排列,看看你能找到什么算法。



如果遇到具体问题,请询问相关问题,我们会尽力而为帮助。但是我们不打算为你做这一切!
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!


一个残酷的方法(有更好的方法)将计算从 0 (m ^ n-1)。在每次迭代时,计数器代表一个有效的骰子配置,你可以检查骰子的总和是否总计

例如

假设你有两个6路骰子(即 n = 2 m = 6 )和总计= 5

然后你必须从 0 迭代到 6 ^ 2-1 = 35

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


这篇关于当m掷骰子被投掷时,你无法获得总和'总数'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆