hdu 1300 Deck

题目

分析:对于n张卡片的最佳摆法,我们只需要在n-1张卡片的摆法下面加一张边缘与桌檐重合的卡片,并将所有卡片一起向桌檐外移动。对于一种最佳摆法,其中心一定在桌檐上,所以一定符合杠杆原理,支点是桌檐。那么对于n张卡片的情况,我们假设第n张向外移动了x,那么前n-1张的重心就在桌檐外x,因为他们的重心在n-1张卡片时原本在桌檐上。第n张卡片的重心在桌檐内0.5-x处,那么我们可以列出杠杆平衡方程(动力*动力臂=阻力*阻力臂):(0.5-x)*1=x*(n-1)

解得:x=1/(2n)。那么我们所要的答案也就是1/2+1/(2×2)+1/(2×3)+...


#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
using namespace std;

int n;

int main()
{
    puts("Cards  Overhang");
    while (~scanf("%d", &n))
    {
        double ans = 0;
        for (int i = 1; i <= n; i++)
            ans += 1.0 / (i * 2);
        printf("%5d%10.3f
", n, ans);    
    }    
    return 0;
}

原文地址:https://www.cnblogs.com/qie-wei/p/10160212.html