zoukankan      html  css  js  c++  java
  • uva 10288 Coupons 概率

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=14&page=show_problem&problem=1229

    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 of n coupons?

    Input

    Input consists of a sequence of lines each containing a single positive integer n, 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 of n 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 ------
       720720

    (Math Lovers’ Contest, Source: University of Alberta Local Contest)
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    #define ll long long
    ll a , s , m , n;
    ll gcd(ll a,ll b) {
        if(a == 0) return b;
        return gcd(b % a , a);
    }
    int wei(ll a) {
        int aa = 0;
        while(a) {
            aa ++;
            a /= 10;
        }
        return aa;
    }
    int main() {
        while(cin >> n) {
            a = 0;
            s = m = 1;
            for(ll i=2;i<=n;i++) {
                s = s * i + m;
                m *= i;
                ll t = gcd(s , m);
                s /= t;
                m /= t;
            }
            ll t = gcd(n , m);
            n /= t; m /= t;
            s *= n;
            a = s / m;
            s %= m;
            if(s == 0) {
                cout << a << endl;
                continue;
            }
            int wa = wei(a) , ws = wei(s) , wm = wei(m);
            for(int i=0;i<=wa;i++) printf(" "); cout << s << endl;
            cout << a; printf(" ");
            for(int i=0;i<wm;i++) printf("-"); puts("");
            for(int i=0;i<=wa;i++) printf(" "); cout << m << endl;
        }
        return 0;
    }
    

      

  • 相关阅读:
    对websoceket进行压力测试(一)
    学习springboot的一个网站
    重装mysql数据库
    websocket扫盲:基础知识(二)
    json-lib 之jsonConfig详细使用
    hibernate的like用法(用占位符解决)
    【转载】hibernate查询参数绑定
    Struts2 Anotation action
    PLSQL怎样导出oracle表结构
    从命令行启动oracle服务
  • 原文地址:https://www.cnblogs.com/tobec/p/p0001.html
Copyright © 2011-2022 走看看