zoukankan      html  css  js  c++  java
  • hdu1330(递推)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1330

    分析:经典问题,n 块相同的木板重叠,最多能够伸出桌面多远

    对于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)。可以推出,如果每块木板都是单位长,那么 n 块木板可以伸出桌面 (1 + 1/2 + 

    1/3 + … + 1/n) / 2 个单位的长度。

    参考:http://gd2011.teacher.com.cn/UserLog/UserLogComment.aspx?UserlogID=15319

    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <iostream>
    #include <algorithm>
    #include <queue>
    #include <cstdlib>
    #include <vector>
    #include <set>
    #include <map>
    #define LL long long
    #define inf 1<<30
    #define mod 1000000007
    #define N 100010
    using namespace std;
    int main()
    {
        int n;
        double a[100010];
        a[1] = 0.5;
        for(int i = 2; i <= 100000; i++)
        a[i] = a[i-1] + 1.0/i/2;
        printf("# Cards  Overhang
    ");
        while(scanf("%d",&n)>0)
        {
            printf("%5d%10.3lf
    ",n,a[n]);
        }
    }
    View Code
  • 相关阅读:
    hlgoj 1766 Cubing
    Reverse Linked List
    String to Integer
    Bitwise AND of Numbers Range
    Best Time to Buy and Sell Stock III
    First Missing Positive
    Permutation Sequence
    Next Permutation
    Gray Code
    Number of Islands
  • 原文地址:https://www.cnblogs.com/lienus/p/4133384.html
Copyright © 2011-2022 走看看