Uva 10288 - Coupons

29 篇文章 0 订阅
15 篇文章 0 订阅

自己写的正数分数类,也作为红书第一题,留念。。。

alpc 加油。


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
#include<cmath>

using namespace std;

#define FOR(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define nMax 1010
#define inf 0x7fffffff
#define LL long long

template<typename T>
T gcd(T a,T b) { return b==0 ? a : gcd(b,a%b); }

template<typename T>
T lcm(T a,T b) { return a==0 ? b : (a==0 ? b :a * b / gcd( a , b ) );  }

template<typename T>
int length(T a){
	int l = 0;
	while(a) { l++ ; a/=10; }
	return l;
}

template<typename T>
struct Fenshu{
	T s,p,q;
	Fenshu() {}
	Fenshu(T s,T p,T q):s(s),p(p),q(q) {}
	void fresh(){
		T d = gcd(p,q);
		p/=d;q/=d;
		if(p>=q){
			s += p/q;
			p %= q;
		}
		if(p == 0) q = 1;
		return ;
	}
	friend Fenshu operator + ( const Fenshu& a, const Fenshu& b) {
		Fenshu c;
		c.s = a.s + b.s;
		c.q = lcm(a.q,b.q);
		c.p = c.q/a.q*a.p + c.q/b.q*b.p;
		c.fresh();
		return c;
	}
	friend Fenshu operator * ( const Fenshu& a, const T& b) {
		Fenshu c;
		c.s = a.s * b;
		c.q = a.q;
		c.p = a.p * b;
		c.fresh();
		return c;
	}
	void out(){
		int ls = length(s),lp = length(p),lq = length(q);
		if(q==1) { printf("%lld\n",s); return ; }
		if(ls) FOR(i,0,ls) printf(" "); printf("%lld\n",p);
		if(ls) printf("%lld ",s); FOR(i,1,max(lp,lq)) printf("-"); printf("\n");
		if(ls) FOR(i,0,ls) printf(" "); printf("%lld\n",q);
	}
};

int main(){
	//freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);
	int n;
	while(~scanf("%d",&n)){
		Fenshu< LL > a(1,0,1);
		FOR(i,2,n){
			a = a + Fenshu< LL > (0,1,i);
		}
		a = a * n;
		a.out();
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值