The number of times and the maximum value of the sum of points in multiple dice

Cast multiple dice, find the number of dice points and how many times they appear the most.

/**
*  投色子,使用递归
*/
#include<stdio.h>
#include <vector>
using namespace std;
int n;              			 //n个色子
int time[100];     				 //点数出现的次数
void tou(int ith, int cnt){
    
          //投第ith个色子,上几个色子投的和是多少
    if(ith > n){
    
        			 //最后一个色子统计出现的次数
        time[cnt]++;
        return ;
    }
    for(int i = 1; i <= 6; i++){
    
    
        tou(ith + 1, cnt + i);
    }
}

int main(){
    
    
    printf("请输入色子的个数1-12:");
    scanf("%d", &n);
    tou(1, 0);
    //打印每个点数和出现的次数
    for(int i = n; i <= n * 6; i++){
    
    
        printf("%d: %d\n",i, time[i]);
    }
    //统计色子中点数和的最大值
    int maxn = 0;
    for(int i = n; i <= n * 6; i++){
    
    
        maxn = max(maxn, time[i]);
    }
    vector<int>ans;
    for(int i = n; i <= n * 6; i++){
    
    
        if(maxn == time[i])
        	ans.push_back(i);
    }
    //打印哪个点出现的次数最多
    printf("答案为:\n");
    for(int i = 0; i < ans.size(); i++){
    
    
        printf("%d ", ans[i]);
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/fuzekun/article/details/105227103