zoukankan      html  css  js  c++  java
  • hdu4180 数论

    一个分数假如 3/5=1/(1+2/3)=1/(1+1/(1+1/2));

    当分子出现1的时候,只要让分母减一。

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #define EX(a, b) (a = a ^ b, b = a ^ b, a = a ^ b)
    using namespace std;
    
    int gcd(int a, int b)
    {
        return (b == 0) ? a : gcd(b, a % b);
    }
    
    class fenshu
    {
        public:
        int x, y;
    
        fenshu friend operator + (fenshu n, fenshu m)
        {
            m.x *= n.y;
            n.x *= m.y;
            m.y *= n.y;
            n.y = m.y;
            n.x += m.x;
            int k = gcd(n.x, n.y);
            n.x /= k;
            n.y /= k;
            return n;
        }
    
        fenshu friend operator / (int k, fenshu n)
        {
            EX(n.x, n.y);
            n.x *= k;
            return n;
        }
    };
    
    fenshu ans_get(fenshu now)
    {
        if(now.x <= 1)
        {
            --now.y;
            return now;
        }
        else
        {
            EX(now.x, now.y);  //小数倒置
    
            fenshu s;
            s.x = now.x / now.y;
            s.y = 1;  //分离出来的整数
    
            now.x = now.x % now.y;
            return 1 / (s + ans_get(now));
        }//继续分离
    }
    
    void solve()
    {
        int T;
        scanf("%d", &T);
        while(T--)
        {
            fenshu now, s;
            scanf("%d/%d", &now.x, &now.y);
            now = ans_get(now);
            printf("%d/%d
    ", now.x, now.y);
        }
    }
    
    int main()
    {
        solve();
        return 0;
    }
  • 相关阅读:
    local http
    redis 存储时间区间的数据
    json 和 jsonp
    ssdb 常用命令行
    php 冒泡排序
    pdo 函数
    异步的消息队列
    php 全局变量$_SERVER
    php 技能树
    get_called_class/get_class方法
  • 原文地址:https://www.cnblogs.com/sweat123/p/4764558.html
Copyright © 2011-2022 走看看