https://vjudge.net/problem/UVA-10288
大街上到处在卖彩票,一元钱一张。购买撕开它上面的锡箔,你会看到一个漂亮的图案。
图案有n种,如果你收集到所有n(n≤33)种彩票,就可以得大奖。
请问,在平均情况下,需要买多少张彩票才能得到大奖呢?
答案以带分数形式输出
例:当n=5时
思路简单,就是输出麻烦
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn=;
struct fraction
{
long long numerator;
long long denominator;
fraction()
{
numerator=;
denominator=;
}
fraction(long long num)
{
numerator=num;
denominator=;
}
fraction(long long a,long long b)
{
numerator=a;
denominator=b;
this->reduction();
}
void operator =(const long long num)
{
numerator=num;
denominator=;
this->reduction();
}
void operator=(const fraction b)
{
numerator=b.numerator;
denominator=b.denominator;
this->reduction();
}
fraction operator + (const fraction b)const
{
long long gcdnum=__gcd(denominator,b.denominator);
fraction tmp=fraction(numerator*(b.denominator/gcdnum)+b.numerator*(denominator/gcdnum),denominator/gcdnum*b.denominator);
tmp.reduction();
return tmp;
}
fraction operator + (const int b)const
{
return ((*this)+fraction(b));
}
fraction operator - (const fraction b)const
{
return ((*this)+fraction(-b.numerator,b.denominator));
}
fraction operator - (const int b)const
{
return ((*this)-fraction(b));
}
fraction operator * (const fraction b)const
{
fraction tmp=fraction(numerator*b.numerator,denominator*b.denominator);
tmp.reduction();
return tmp;
}
fraction operator * (const int b)const
{
return ((*this)*fraction(b));
}
fraction operator / (const fraction b)const
{
return ((*this)*fraction(b.denominator,b.numerator));
}
void reduction()
{
if(numerator==)
{
denominator=;
return;
}
long long gcdnum=__gcd(numerator,denominator);
numerator/=gcdnum;
denominator/=gcdnum;
}
void print()
{
if(denominator==) { printf("%lld\n",numerator); }
else
{
long long num=numerator/denominator;
long long tmp=num;
int len=;
while(tmp)
{
len++;
tmp/=;
}
for(int i=;i<=len;i++) printf(" ");
if(len) printf(" ");
printf("%lld\n",numerator%denominator);
if(num) printf("%lld ",num);
tmp=denominator;
while(tmp)
{
printf("-"); tmp/=;
}
puts("");
for(int i=;i<=len;i++) printf(" ");
if(len) printf(" ");
printf("%lld\n",denominator);
}
}
}f[maxn];
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
f[]=;
for(int i=;i<=n;i++)
f[i]=f[i-]+fraction(,i);
f[n]=f[n]*n;
f[n].print();
}
return ;
}