zoukankan      html  css  js  c++  java
  • uva 10288 gailv

    Problem F
    
    Coupons
    
    Input: standard input
    
    Output: standard output
    
    Time Limit: 2 seconds
    
    Memory Limit: 32 MB
    
     
    
    Coupons in cereal boxes are numbered 1 to n, and a set of one of each is required for a prize (a cereal box, of course). With one coupon per box, how many boxes on average are required to make a complete set ofn coupons?
    
    Input
    
    Input consists of a sequence of lines each containing a single positive integern, 1<=n<=33, giving the size of the set of coupons. Input is terminated by end of file.
    
    Output
    
    For each input line, output the average number of boxes required to collect the complete set ofn coupons. If the answer is an integer number, output the number. If the answer is not integer, then output the integer part of the answer followed by a space and then by the proper fraction in the format shown below. The fractional part should be irreducible. There should be no trailing spaces in any line of output.
    
    Sample Input
    
    2
    5
    17
    Sample Output
    
    3 
       5
    11 --
       12
       340463
    58 ------

    记得每次求gcd不然会爆掉。 思路:到k张时。我们还有n-k张没有得到。所以我们得到的概率为n-k/n;

    所以到k+1步的期望时n/n-k;

    整理得 n* 1/i(n>i>1)的累加

    #include <iostream>
    #include <string.h>
    #include <stdio.h>
    #include <algorithm>
    using namespace std;
    #define ll long long
    ll gcd(ll n,ll m)
    {
        if(m==0)
            return n;
        return gcd(m,n%m);
    }
    int geth(ll x)
    {
        int ans=0;
        while(x>0)
        {
            x=x/10;
            ans++;
        }
        return ans;
    }
    int main()
    {
        ll  n;
        while(~scanf("%lld",&n))
        {
            
            ll fenmu=1;
            ll temp=1;
            for(int i=1;i<=n;i++)
            {
                temp=gcd(fenmu,i);
                fenmu=fenmu*i/temp;
            }
            ll fenzi=0;
            for(int i=1;i<=n;i++)
            {
                fenzi+=fenmu/i;
                
            }
            fenzi=fenzi*n;
            ll p=gcd(fenzi,fenmu);
            fenzi/=p;
            fenmu/=p;
            ll t=fenzi/fenmu;
            if(fenzi-fenmu*t>0)
            {
                if(t!=0)
                {
                    ll a=geth(t);
                    for(int i=0;i<=a;i++)
                    {cout<<" ";}
                    cout<<fenzi-fenmu*t;
                    ll b=geth(fenzi-fenmu*t);
                    ll c=geth(fenmu);
                    if(b>c)
                        c=b;
                    cout<<endl;
                    cout<<t<<" ";
                    for(int i=1;i<=c;i++)
                    {cout<<"-";}
                    cout<<endl;
                    for(int i=0;i<=a;i++)
                    {cout<<" ";}
                    cout<<fenmu<<endl;
                }
            }
            else
                cout<<t<<endl;
            
        }
        
        
        return 0;
    }
  • 相关阅读:
    表单验证
    obs 之 OBSObj
    rtmp流媒体协议分析(h264、aac)
    lintcode 508.Wiggle Sort
    SVN备份批处理文件
    防火墙没关导致 ORA-12541: TNS: 无监听程序
    [转]window10系统安装oracle11g时遇到INS-13001环境不满足最低要求
    关键驱动因素、约束和浮动因素
    C#之虚函数 非常清晰全面的讲解
    今天有个朋友问我抽象方法和接口的区别,为了解释清楚这个事情,我在网上看到一篇文章讲的非常好给大家分享一下,也感谢原作者的付出
  • 原文地址:https://www.cnblogs.com/2014slx/p/9538251.html
Copyright © 2011-2022 走看看