UVA10288 Coupons(卡输出神题)

来源:互联网 发布:javaweb购物网站源码 编辑:程序博客网 时间:2024/06/10 21:30

给出n,求Σn/i (i = 1 to n,n<=33)

分数的基本运算,首先打好表。

#include<cstdio>using namespace std;typedef long long LL;struct T{LL zs,fz,fm;//整数,分子,分母T(){zs = 0,fz = 0,fm = 1;}T(LL a,LL b,LL c){zs = a;fz = b;fm = c;}}ans[50];LL gcd(LL a,LL b){if(b == 0) return a;else return gcd(b,a%b);}T add(T a,T b){a.zs += a.fz/a.fm;b.zs += b.fz/b.fm;a.fz %= a.fm;b.fz %= b.fm;T temp;LL dd = gcd(a.fz*b.fm + a.fm*b.fz,a.fm*b.fm);temp.zs = a.zs+b.zs;temp.fz = (a.fz*b.fm + a.fm*b.fz)/dd;temp.fm = a.fm*b.fm/dd;temp.zs += temp.fz/temp.fm;temp.fz %= temp.fm;return temp;}void init(){for(int n = 1; n <= 33; n++)for(int i = 1; i <= n; i++)ans[n] = add(ans[n],T(0,n,i));}int getlen(LL x){int len = 0;while(x){len++;x /= 10;}return len;}void print(T x){if(x.fz == 0) {printf("%lld\n",x.zs); return;}for(int i = 1; i <= getlen(x.zs)+1; i++)printf(" ");printf("%lld\n",x.fz);printf("%lld ",x.zs);for(int i = 1; i<= getlen(x.fm); i++)printf("-");printf("\n");for(int i = 1; i <= getlen(x.zs)+1; i++)printf(" ");printf("%lld\n",x.fm);}int main(){init();int n;while(scanf("%d",&n) != EOF){print(ans[n]);}}


0 0
原创粉丝点击