• UVA 10288 Coupons


    #include<bits/stdc++.h>
    #include<stdio.h>
    #include<iostream>
    #include<cmath>
    #include<math.h>
    #include<queue>
    #include<set>
    #include<map>
    #include<iomanip>
    #include<algorithm>
    #include<stack>
    #define inf 0x3f3f3f3f
    using namespace std;
    typedef long long ll;
    ll gcd(ll a,ll b)
    {
        return b==0?a:gcd(b,a%b);
    }
    //分数类
    struct Fraction{
        long long num;
        long long den;
        Fraction(long long num=0,long long den=1){
            if(den<0){
                num=-num;
                den=-den;
            }
            assert(den!=0);
            long long g=gcd(abs(num),den);
            this->num=num/g;
            this->den=den/g;
        }
        Fraction operator+(const Fraction&o)const {
            return Fraction(num*o.den+den*o.num,den*o.den);
        }
        Fraction operator-(const Fraction&o)const{
            return Fraction(num*o.den-den*o.num,den*o.den);
        }
        Fraction operator*(const Fraction &o)const{
            return Fraction(num*o.num,den*o.den);
        }
        Fraction operator/(const Fraction&o)const{
            return Fraction(num*o.den,den*o.num);
        }
        bool operator<(const Fraction &o)const{
            return num*o.den<den*o.num;
        }
        bool operator==(const Fraction&o)const{
            return num*o.den==den*o.num;
        }
    };
    
    Fraction f[35];
    Fraction ans;
    int len(ll n)
    {
        if(n==0)return 1;
        int res=0;
        while(n>0)
        {
            n/=10;
            res++;
        }
        return res;
    }
    void print()
    {
        if(ans.num%ans.den==0)
        {
            printf("%lld
    ",ans.num/ans.den);
        }
        else
        {
            ll a=ans.num/ans.den;
            ans.num-=a*ans.den;
            int l;//空格长度
            l=len(a)+1;
            while(l--)printf(" ");
            printf("%lld
    ",ans.num);
            printf("%lld ",a);
            l=len(ans.den);
            while(l--)printf("-");
            printf("
    ");
            l=len(a)+1;
            while(l--)printf(" ");
            printf("%lld
    ",ans.den);
        }
    }
    int main()
    {
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
    #endif // ONLIN23
        int n;
        while(cin>>n)
        {
            ans.den=1;ans.num=0;
            for(int i=1;i<=n;i++)f[i]=Fraction(n,i);
            for(int i=1;i<=n;i++)ans=f[i]+ans;
            print();
        }
    }
  • 相关阅读:
    浏览器内核
    link和@import的区别
    跨域iframe如何通信
    数组、对象基本操作
    浏览器兼容性问题
    微信H5开发
    前端面试整理
    js多维数组转一维数组
    js实现继承
    javascript函数闭包(closure)
  • 原文地址:https://www.cnblogs.com/linruier/p/9532569.html
Copyright © 2020-2023  润新知