码迷,mamicode.com
首页 > 其他好文 > 详细

UVA 10288 Coupons

时间:2015-10-06 06:58:43      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

#include <iostream>
#include <stdio.h>
#include <cstring>
#define N 34
typedef long long LL;
using namespace std;

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;
    }
    void Add(struct T t)
    {
        zs += t.zs;
        fz = fz*t.fm+t.fz*fm;
        fm = fm*t.fm;
        Standard();
    }
    void Standard()
    {
        zs += fz/fm;
        fz = fz%fm;
        LL g=gcd(fz,fm);
        fz = fz/g;
        fm = fm/g;
    }
    LL gcd(LL a, LL b)
    {
        return b==0?a:gcd(b,a%b);
    }
};

struct T ans[N];

void Init()
{
    for(int i=1;i<N;i++)
    {
        for(int j=1;j<=i;j++)
        {
            ans[i].Add(T(0,i,j));
        }
    }
}

int Len(LL a)
{
    int len=0;
    while(a)
    {
        len++;
        a /= 10;
    }
    return len;
}

void Print(struct T t)
{
    int i;
    if(t.fz==0)
        printf("%lld\n", t.zs);
    else
    {
        for(i=0;i<Len(t.zs)+1;i++)
            printf(" ");
        printf("%lld\n", t.fz);
        printf("%lld ", t.zs);
        for(i=0;i<Len(t.fm);i++)
            printf("-");
        printf("\n");
        for(i=0;i<Len(t.zs)+1;i++)
            printf(" ");
        printf("%lld\n", t.fm);
    }
}

int main()
{
    int n;
    Init();
    while(scanf("%d",&n)!=EOF)
    {
        Print(ans[n]);
    }
    return 0;
}

 

UVA 10288 Coupons

标签:

原文地址:http://www.cnblogs.com/tuty/p/4856625.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!