zoukankan      html  css  js  c++  java
  • leetcode-剑指60-OK

    // language c
    // 剑指60
    // https://leetcode-cn.com/problems/nge-tou-zi-de-dian-shu-lcof/
    
    
    /**
     * Note: The returned array must be malloced, assume caller calls free().
     */
    double* dicesProbability(int n, int* returnSize){
    	double ans[6*n+1];	// 用来暂时存储答案
    	ans[1] = 1.0/6.0;
    	ans[2] = 1.0/6.0;
    	ans[3] = 1.0/6.0;
    	ans[4] = 1.0/6.0;
    	ans[5] = 1.0/6.0;
    	ans[6] = 1.0/6.0;      //第一波
    
    
    	// 检查是b否在a的圈内,在的话返回true
    	bool check(int a,int b){
    		if((b >= a) && (b <=6*a))
    			return true;
    		return false;
    	}
    
    
    	for(int i =2; i<=n; i++){	// 每次循环是算i个骰子的答案,会占有i~6i的位置,i从2算到n
    
    		double bns[5*i+1];	// 用来暂时存储, 0号存的值代表i的概率
    		for(int j=0; j<5*i+1; j++)		// 初始化
    			bns[j] = 0.0;
    
    		for(int j = i; j<=6*i; j++){	// 算某个和的概率
    			for( int k = 1; k<=6; k++){ 
    				if(check(i-1,j-k))
    					bns[j-i] += ans[j-k]/6.0;
    			}
    		}
    
    		for(int j=0; j<5*i+1; j++)		// 填回去
    			ans[i+j] = bns[j];
    	}
    
    
    	// 填回去了,准备返回答案,这步不会错的
    	returnSize[0] = 5*n+1;
    	double* AAA = (double *)malloc(sizeof(double) * (5*n+1));
    	for(int i = 0; i<5*n+1; i++)
    		AAA[i] = ans[i+n];
    
    	return AAA;
    }
    
  • 相关阅读:
    HTML5: HTML5 Video(视频)
    HTML5: HTML5 Geolocation(地理定位)
    HTML5: HTML5 拖放
    HTML5: HTML5 MathML
    HTML5: HTML5 内联 SVG
    HTML5: HTML5 Canvas
    HTML5: HTML5 新元素
    HTML5: 浏览器支持
    HTML5: HTML5 介绍
    HTML5: 目录
  • 原文地址:https://www.cnblogs.com/gallien/p/14349043.html
Copyright © 2011-2022 走看看