UVA 10288 Coupons

xiaoxiao2021-03-01  4

#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\n",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\n",ans.num); printf("%lld ",a); l=len(ans.den); while(l--)printf("-"); printf("\n"); l=len(a)+1; while(l--)printf(" "); printf("%lld\n",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(); } }

 

转载请注明原文地址: https://www.6miu.com/read-3100192.html

最新回复(0)