Codeforces 102263G-Card Game【数学】 难度:**

题意:

Zeyad and Ehab are playing a simple card game, each player initially has a deck of n cards numbered from 1 to n, the game lasts n turns and in each turn both players privately choose one card and then both players reveal the chosen cards, the player that chose the card with the higher number earns points equal to that number, for example if Ehab chooses 4 and Zeyad chooses 6 Zeyad earns 6 points, if the numbers chosen are equal no one earns any point, the cards chosen do not return to the deck, they are discarded instead.

Ehab doesn’t care about winning or losing, he cares about mathematical values of the game, specifically he wants to know the expected number of points he’ll earn considering all possible outcomes, can you help him?

Input
The first and only line of input contains a single integer n(1≤n≤106).

Output
Print a real number, the expected number of points Ehab will earn in the game.

The answer is considered correct if the absolute or relative error is not greater than 10−6.

Examples
inputCopy
3
outputCopy
2.6666666667
inputCopy
7
outputCopy
16.0000000000

题解:

首先我们知道,你手中的牌大小分别为1~n,那么与对方的卡牌匹配,一共就会有Ann种情况。那么考虑其中的某一张牌k,它在每个位置出现的次数都是A(n-1)(n-1),而只有它对应的牌比自身要小的情况才能够得分,因此对于k这张牌,只有k-1个位置是可以得分的。而每个位置都会出现A(n-1)(n-1)次,因此对于k这张牌,它能贡献的总分数是A(n-1)(n-1)×(k-1)×k。
因此最后的结果就是[(n-1)!×1×2+(n-1)!×2×3+…+(n-1)!×(n-1)×n]/n!,化简后就是:
(1×2+2×3+…+(n-1)×n)/n。

代码:

#include<stdio.h>
int main()
{
	double n,sum=0.0;
	scanf("%lf",&n);
	for(double i=1.0;i<=n-0.5;i+=1.0)sum+=i*(i+1)/n;
	printf("%.10lf\n",sum); 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值