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 it A
.
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.
He has n
One day he decides to pick two balls from the box. First he randomly picks a ball from the box, and names it A
If the number written on A
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 a
i![]()
,
denoting the numbers on the balls.
(1≤t≤300,2≤n≤300,1≤a
i
≤300
)
t
(1≤t≤300,2≤n≤300,1≤a
Output
For each testcase, output a real number with 6 decimal places.
Sample Input
2 3 1 2 3 3 100 100 100Sample Output0.500000 0.000000问题描述DZY喜欢玩球。 他有n个球,装进一个大盒子里。每个球上面都写着一个整数。 有一天他打算从盒子中挑两个球出来。他先均匀随机地从盒子中挑出一个球,记为A。他不把A放回盒子,然后再从盒子中均匀随机地挑出一个球,记为B。 如果A上的数字严格大于B上的数字,那么他就会感到愉悦。 现在告诉你每个球上的数字,请你求出他感到愉悦的概率是多少。输入描述第一行t,表示有t组数据。 接下来t组数据。每组数据中,第一行包含一个整数n,第二行包含n个用空格隔开的正整数ai,表示球上的数字。 (1≤t≤300,2≤n≤300,1≤ai≤300)输出描述对于每个数据,输出一个实数答案,保留6位小数。输入样例2 3 1 2 3 3 100 100 100输出样例0.500000 0.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; }