您的位置:首页 > 编程语言 > Go语言

hdu 5645 DZY Loves Balls

2016-03-25 17:02 483 查看
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5645

题       意:有n个球,放在一个箱子里,球的编号为1~n,先后无放回的挑出两个球为球A和球B,问A的编号严格大于球B的概率是多少?

思       路:对于每一个数求出在数组中比他大的数的个数在相加,最后除以总的可能的情况n*(n-1)即可(不能以去重后排序的数组来求满足条件的情况)

代码如下:

#include <stdio.h>
#include <iostream>
#include <cstring>
#include <sstream>
#include <set>
#include <algorithm>
using namespace std;
typedef long long LL;

int v[330];
int main()
{
int T;
scanf ( "%d", &T );
while( T-- )
{
int n;
scanf ( "%d", &n );
for( int i = 0; i < n; i ++ )
{
scanf ( "%d", &v[i] );
}
sort( v, v+n );
int he = n*(n-1);
int sum = 0;
for( int i = 0; i < n; i ++ )
for( int j = i+1; j < n; j ++ )
if( v[i] < v[j] ) sum++;
if( sum == 0 ) printf("0.000000\n");
else
printf("%.6lf\n",double(sum)/double(he) );
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  algorithm