HDU 4503

来源:互联网 发布:ubuntu启动进入命令行 编辑:程序博客网 时间:2024/06/01 22:01

湫湫系列故事——植树节

老是刷水题,哈哈。练练手嘛,我不是专业人士。

这是一道概率题,我从来没写过概率题,所以没有解题思路。按概率论的思路来的话会很麻烦,我想了一会就放弃了。然后去看了看网上的题解,是这样说的:

从组合的角度来说,每个人又bi个朋友,也就是说有n-1-bi个人不是朋友,那么三个人不是都是朋友和都不是朋友的组合有bi*(n-1-bi)种。累加除以二(因为对于一种情况两个人都算了一遍)就是总组合数。

#include <cstdio>#include <cstring>using std::memset;int t, n;int C(int n, int m){    int ans = 1 ;    for(int i = n-m+1 ; i <= n; i++)    {        ans *= i;    }    for(int i = 1; i <= m; i++)    {        ans /= i;    }    return ans;}int main(){    scanf("%d", &t);    while(t--)    {        scanf("%d", &n);        int c = 0;        for(int i = 0 ; i < n; i++)        {            int temp;            scanf("%d", &temp);            c += temp * (n - 1 - temp);        }        printf("%.3f\n", 1-c*1.0/C(n,3)/2);    }}


原创粉丝点击