Sword refers to offer-43-n dice points-java

Questions and tests

package sword043;
/* 题目:把n个骰子仍在地上,所有骰子朝上一面的点数之和为s,输入n,打印出s的所有可能的值出现的概率。

*/

import java.util.List;

public class main {
	
	public static void main(String[] args) {
		int [] testTable = {0,6,3};
		for (int ito : testTable) {
			test(ito);
		}
	}
		 
	private static void test(int ito) {
		Solution solution = new Solution();
		long begin = System.currentTimeMillis();
		System.out.print(ito);		    
		System.out.println();
		//开始时打印数组
		
		solution.printProbability(ito);//执行程序
		long end = System.currentTimeMillis();	
		

		System.out.println();
		System.out.println("耗时:" + (end - begin) + "ms");
		System.out.println("-------------------");
	}

}

Solution 1: Based on recursion to find the number of dice, the time efficiency is not high enough

Now we consider how to count the number of occurrences of each point. To find the sum of the points of n dice, you can first divide the n dice into two piles: the first pile has only one, and the other has n-1. The single one 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 sum of points. Next, divide the remaining n-1 dice into two piles, the first pile has only one, and the second pile has n-2. We add the number of individual dice in the previous round to the number of individual dice in this round, and then add n-2 dice to calculate the sum of the points. At this point of analysis, it is not difficult to find that this is a recursive idea, the condition for the end of recursion is that there is only one dice left in the end.

Solution 2: Find the number of dice based on the loop, with good time performance

To solve this problem in another way, we can consider using two arrays to store the number of occurrences of each summary of the dice points. In a loop, the nth number in each array represents the number of times the dice sum is n. In the next round of the loop, we add a new dice, and the sum is the number of occurrences of n. In the next round, we add a new dice. At this time, the number of appearances of the dice with the sum of n should be equal to the sum of the dice points in the previous cycle, which is n-1, n-2, n-3, n-4, n The sum of the times of -5, so we set the nth number of another array to the n-1, n-2, n-3, n-4, n-5 corresponding to the previous array 

public class Solution {
	
    public void printProbability(int n) {
    	if(n<=0) {
    		return;
    	}
    	int total = 6 * n;
    	int[] arrayNow = new int[total + 1];
    	int[] arrayNext = new int[total + 1];
    	for(int i=1;i<7;i++) {
    		arrayNow[i] = 1;
    	}
    	for(int k = 2;k<=n;k++) {
    		for(int i=1;i<=6*k-6;i++) {
    			for(int j=1;j<=6;j++) {
    				arrayNext[i+j] += arrayNow[i];
    			}
    		}
    		arrayNow = arrayNext;
    		arrayNext = new int[total + 1];
    	}
    	double totalProbability = Math.pow(6, n);
    	for(int i=1;i<=6*n;i++) {
    		double ratio = (double)arrayNow[i]/totalProbability;
    		System.out.println(i+"  "+ratio);
    	}
    	
    }
    
  
}

 

Guess you like

Origin blog.csdn.net/xushiyu1996818/article/details/112282090