ACM Coupons

//Coupons
/*
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


*/
//算法分析,假设当前已经有k种Coupons,那么获得新的Coupons的概率为(n-k)/n,所需要的步数的期望值是(n)/(n-k)
//那么得到n种Coupons的期望就是其和,即n/n+n/(n-1)+n/(n-2)+.....+n/1
#include<iostream>
using namespace std;
typedef long long LL;
LL Gcd(LL A,LL B){
LL Tmp;
/*
while (B>0){
Tmp = A%B;
A = B;
B = Tmp;
}
return A;
*/
//Or Just below:
return A%B>0?Gcd(B,A%B):B;
}


class Fraction{
public:
Fraction(){
num = 0;
deno = 1;
}
Fraction(LL n, LL d=1){
if (d < 0){
n = -n;
d = -d;
}
num = n;
deno = d;
LL tmp = Gcd(num, deno);
num =num / tmp;
deno = deno / tmp;
}
Fraction operator +(const Fraction &plus)const{
return Fraction(num*plus.deno+deno*plus.num,deno*plus.deno);
}
Fraction operator -(const Fraction &minus)const{
return Fraction(num*minus.deno - deno*minus.num, deno*minus.deno);
}
int deno;//分母
int num;// 分子
};

int main(){
int n;

while (cin >> n){
Fraction Sum(0,1);
int i = 0;
while (i<n){
Sum = Sum + Fraction(n,n-i);
i++;
}
//cout << Sum.num << " " << Sum.deno << endl;
LL left = Sum.num/Sum.deno;
LL top = Sum.num%Sum.deno;


if (top != 0){
for (int j = left; j>0; j /= 10) cout << " ";
cout << " "<<top << endl;
cout << left;
for (int j = Sum.deno; j > 0; j /= 10)cout << "-";
cout << endl;
for (int j = left; j >0; j /= 10) cout << " ";
cout << " " << Sum.deno << endl;
}
else
cout << left << endl;


}


return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值