The sword refers to the offer--43.n points of dice

Question: Throw n dice on the ground, the sum of the points on the upside of all dice is s, enter n, and print out the probability of all possible values ​​of s appearing

Ability Interpretation: Abstract modeling, the first step is to choose a reasonable data structure to express the problem, the second step is to analyze the internal laws in the model, and express the laws in programming languages

Analysis: The minimum value of the sum of n dice points is n, the maximum value is 6n, and the number of permutations of all points is 6^n, so to solve this problem, you can first count the number of occurrences of each point, and then put each point Divide the number of occurrences of points by 6^n to find the probability of occurrence of each point

Method 1: Recursion, find the sum of the points of n dice, and divide the n dice into two piles: the first pile has only 1, the second pile has n-1, and the first pile may have points from 1 to 6 , we need to calculate the number of points from 1 to 6 and the remaining n-1 dice to calculate the point sum, and then divide the remaining n-1 dice into two piles: the first pile has only 1, the first The second pile has n-2, and so on. The condition for the end of the recursion is that there is only one dice left at the end. But the recursive implementation has many computations that are repeated, resulting in slow performance when the number gets larger

Method 2: Loop, use two arrays to store the number of occurrences of each total number of dice points. In a loop, the nth number in the first array represents the number of occurrences of the sum of the dice and n. In the next loop, add On a new dice, the number of occurrences of the dice with the sum of n at this time is equal to the number of dice points in the previous cycle and the sums of n-1, n-2, n-3, n-4, n-5, n-6 times. Sum, ie f(n)=f(n-1)+f(n-2)+f(n-3)+f(n-4)+f(n-5)+f(n-6)

public class wr43printProbability {
// recurse
	private static final int maxValue=6;
	
	public static void printProbability(int number){//There are n dice
		if(number<1){
			return ;
		}
		int maxSum=number*maxValue;//The maximum value of sum is 6n, and the minimum value is n
		int [] prob=new int[maxSum-number+1];//Create an array with a length of 6n-n+1, ​​and save the number of occurrences of the point number s to the sn-th element of the array
		for(int i=number;i<=maxSum;i++){
			prob[i-number]=0;
		}
		
		double total=Math.pow(maxValue, number);//n dice, each with 6 faces, a total of 6^n combinations
		probability(number,prob);//Count the number of occurrences of each case from n to 6n
		for(int i=number;i<=maxSum;i++){
			double ratio=prob[i-number]/total;
			System.out.println("i: "+i+" ratio: "+ratio);
		}
	}
	
	public static void probability(int number,int []prob){
		for(int i=1;i<=maxValue;i++){//Start from the first dice
			probability(number,number-1,i,prob);
		}
	}
// Find the sum of the points of n dice, and divide the n dice into two piles: the first pile has only 1, and the second pile has n-1
// The first pile may have 1 to 6 points, we need to calculate each point from 1 to 6 and the remaining n-1 dice to calculate the point sum
// Next divide the remaining n-1 dice into two piles: the first pile has only 1, and the second pile has n-2
// And so on, recursive thinking, the condition for the end of the recursion is that there is only one dice left at the end
	public static void probability(int original,int current,int sum,int []prob){
// The total number of parameters original dice, current is the current sieve, sum is the current sum, prob is the array throughout
		if(current==0){
			prob[sum-original]++;
		}
		else{
			for(int i=1;i<=maxValue;i++){
				probability(original,current-1,sum+i,prob);
			}
		}
	}
	
// loop
	public static void printProbabilityCicle(int number){
		if(number<1){
			return ;
		}
		int maxSum=number*maxValue;
		int [] [] prob = new int [2] [maxSum + 1];
		
		int flag=0;
		for(int i=1;i<=maxValue;i++){
			prob[flag][i]=1;
		}
		for(int k=2;k<=number;k++){
			flag=1-flag;
			for(int i=1;i<k;i++){
				prob[flag][i]=0;
			}
			for(int i=k;i<=maxValue*k;i++){
				int count=1;
				prob[flag][i]=0;
				while(i-count>0 && count<= 6){
					prob[flag][i]+=prob[1-flag][i-count];
					count++;
				}
			}
		}
		double total=Math.pow(maxValue, number);
		for(int i=number;i<=maxSum;i++){
			double ratio=prob[flag][i]/total;
			System.out.println("i: "+i+" ratio: "+ratio);
		}
	}
	public static void main(String []args){
// printProbability (2);
		printProbabilityCicle (2);
	}
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325569187&siteId=291194637