您的位置:首页 > 其它

杭电5645之DZY Loves Balls

2016-07-23 14:49 253 查看
[align=left]Problem Description[/align]
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.
 

[align=left]Input[/align]
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.

(1≤t≤300,2≤n≤300,1≤ai≤300)
 

[align=left]Output[/align]
For each testcase, output a real number with 6 decimal places.

 

[align=left]Sample Input[/align]

2
3
1 2 3
3
100 100 100[align=left]Sample Output[/align]
0.500000
0.000000
问题描述
DZY喜欢玩球。

他有nn个球,装进一个大盒子里。每个球上面都写着一个整数。

有一天他打算从盒子中挑两个球出来。他先均匀随机地从盒子中挑出一个球,记为AA。他不把AA放回盒子,然后再从盒子中均匀随机地挑出一个球,记为BB。

如果AA上的数字严格大于BB上的数字,那么他就会感到愉悦。

现在告诉你每个球上的数字,请你求出他感到愉悦的概率是多少。
输入描述
第一行tt,表示有tt组数据。

接下来tt组数据。每组数据中,第一行包含一个整数nn,第二行包含nn个用空格隔开的正整数a_ia​i​​,表示球上的数字。

(1\le t\le 300, 2\le n \le 300,1\le a_i \le 3001≤t≤300,2≤n≤300,1≤a​i​​≤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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: