当前位置: 首页 > 工具软件 > card-one > 使用案例 >

Card Game

胡国兴
2023-12-01

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
Input
3
Output
2.6666666667
Input
7
Output
16.0000000000

思路:对于选择的i,只有对手选择1~i-1时才能得分,所以期望是对于所有i,i*(i-1)/n的和。

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,i;
    double ans=0;
    scanf("%d",&n);
    for(i=1; i<=n; i++)
    {
        ans=ans+i*1.0*(i-1)/n;
    }
    printf("%.6lf",ans);
    return 0;
}
 类似资料:

相关阅读

相关文章

相关问答