百事世界杯之旅

百事世界杯之旅

博客园的 \(Markdown\) 不支持分数!!!

无语啊。。。公式推导在 这里

#include <cstdio>
#include <iostream>
using namespace std;

typedef long long ll;

ll gcd(ll a,ll b)
{
    return !b?a:gcd(b,a % b);
}

struct Frac
{
    ll a,b,z;
    Frac(ll A=0,ll B=1):a(A),b(B){}
    Frac operator + (const Frac& x)
    const{
        ll C=b*x.b,D=a*x.b+x.a*b;
        ll g=gcd(max(C,D),min(C,D));
        return Frac(D/g,C/g);
    }
};
inline int Len(ll x)
{
    if(x==0)return 0;
    int l=0;
    while(x)
    {
        l++;
        x/=10;
    }
    return l;
}
int main()
{
    ll n;
    cin>>n;
    Frac E(0,1);
    for(int i=1;i<=n;i++)E=E+Frac(n,(ll)i);
    if(E.b==1LL)cout << E.a;
    else
    {
        ll C=E.a/E.b,A=E.a%E.b,B=E.b;
        int l1=Len(C),l2=Len(B);
        for(int i=1;i<=l1;i++)cout << " ";
        cout << A << endl;
        if(C != 0)cout << C;
        for(int i=1;i<=l2;i++)cout << '-';
        cout << endl;
        for(int i=1;i<=l1;i++)cout << " ";
        cout << B;
        //当 C=0 时的情况。 
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/zhinv/p/9649023.html