杭电5645之DZY Loves Balls

来源:互联网 发布:unity3d 引擎 编辑:程序博客网 时间:2024/06/07 21:59
Problem Description
DZY loves playing balls.

He has n balls in a big box. On each ball there is an integer written.

One day he decides to pick two balls from the box. First he randomly picks a ball from the box, and names itA. Next, without putting A back into the box, he randomly picks another ball from the box, and names it B.

If the number written on A is strictly greater than the number on B, he will feel happy.

Now you are given the numbers on each ball. Please calculate the probability that he feels happy.
 

Input
First line contains t denoting the number of testcases.

t testcases follow. In each testcase, first line contains n, second line contains n space-separated positive integers ai, denoting the numbers on the balls.

(1t300,2n300,1ai300)
 

Output
For each testcase, output a real number with 6 decimal places.
 

Sample Input
231 2 33100 100 100
Sample Output
0.5000000.000000

问题描述
DZY喜欢玩球。他有nn个球,装进一个大盒子里。每个球上面都写着一个整数。有一天他打算从盒子中挑两个球出来。他先均匀随机地从盒子中挑出一个球,记为AA。他不把AA放回盒子,然后再从盒子中均匀随机地挑出一个球,记为BB。如果AA上的数字严格大于BB上的数字,那么他就会感到愉悦。现在告诉你每个球上的数字,请你求出他感到愉悦的概率是多少。
输入描述
第一行tt,表示有tt组数据。接下来tt组数据。每组数据中,第一行包含一个整数nn,第二行包含nn个用空格隔开的正整数a_iai,表示球上的数字。(1\le t\le 300, 2\le n \le 300,1\le a_i \le 3001t300,2n300,1ai300)
输出描述
对于每个数据,输出一个实数答案,保留6位小数。
输入样例
231 2 33100 100 100
输出样例
0.5000000.000000

分析:把所有球两两比较,第一个球大于第二个球的情况除以总情况n*(n-1),即得答案

AC代码如下:

#include "stdio.h"int main(int argc, char* argv[]){int n,i,j,m,a[301],num;double res;scanf("%d",&n);while(n--){num=0;scanf("%d",&m);for (i=0;i<m;i++){scanf("%d",&a[i]);}for (i=0;i<m;i++){for (j=0;j<m;j++){if (a[i]>a[j]){num++;//记录A>B的情况}}}res=(double)num/(m*(m-1));printf("%lf\n",res);}return 0;}


0 0
原创粉丝点击