zoukankan      html  css  js  c++  java
  • HDU 1099 Lottery

    Lottery

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 4348    Accepted Submission(s): 1934


    Problem Description
    Eddy's company publishes a kind of lottery.This set of lottery which are numbered 1 to n, and a set of one of each is required for a prize .With one number per lottery, how many lottery 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<=22, giving the size of the set of coupons.
     
    Output
    For each input line, output the average number of lottery 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 ouput.
     
    Sample Input
    2
    5
    17
     
    Sample Output
    3
        5
    11 --
        12
         340463
    58 ------
         720720
     
     
     1 #include<iostream>
     2 #include<string>
     3 using namespace std;
     4 int main()
     5 {
     6     int n,i,j,a,b,c;
     7     int fz[23]={0},fm[23]={0},zs[23]={0};
     8     int sum=1, gbs=1;
     9     zs[1]=1;
    10     for(i=2; i<=22; i++)
    11     {
    12         for(j=1; j<=i; j++)
    13         {
    14             if(j*gbs%i==0)
    15             {
    16                 gbs=j*gbs;
    17                 sum=j*sum+gbs/i;
    18                 fm[i]=gbs/i;
    19                 zs[i]=sum/fm[i];
    20                 fz[i]=sum%fm[i];
    21                 a=fm[i];
    22                 b=fz[i];
    23                 while(b!=0)
    24                 {
    25                     c=a%b;
    26                     a=b;
    27                     b=c;
    28                 }
    29                 fz[i]=fz[i]/a;
    30                 fm[i]=fm[i]/a;
    31                 break;
    32             }
    33         }
    34     }
    35     while(cin>>i)
    36     {
    37         if(fz[i])
    38         {
    39             int lin=zs[i], len=2, num=1;
    40             while(lin/10)
    41             {
    42                 lin/=10;
    43                 len++;
    44             }
    45             lin=fm[i];
    46             while(lin/10)
    47             {
    48                 lin/=10;
    49                 num++;
    50             }
    51             cout<<string(len, ' ')<<fz[i]<<endl;
    52             cout<<zs[i]<<' '<<string(num, '-')<<endl;
    53             cout<<string(len, ' ')<<fm[i]<<endl;
    54         }
    55         else
    56         {
    57             cout<<zs[i]<<endl;
    58         }
    59     }
    60     return 0;
    61 }
  • 相关阅读:
    LeetCode Flatten Binary Tree to Linked List
    LeetCode Longest Common Prefix
    LeetCode Trapping Rain Water
    LeetCode Add Binary
    LeetCode Subsets
    LeetCode Palindrome Number
    LeetCode Count and Say
    LeetCode Valid Parentheses
    LeetCode Length of Last Word
    LeetCode Minimum Depth of Binary Tree
  • 原文地址:https://www.cnblogs.com/zhaopeng938/p/9418703.html
Copyright © 2011-2022 走看看