zoukankan      html  css  js  c++  java
  • poj1079

    注意输入1 2的情况,第一个结果为1/1,而不是0/1。题中没说,但是实际上要求对于同一个分母有两个同样好的分子时,取较大的。

    View Code
    #include <iostream>
    #include
    <cstdlib>
    #include
    <cstring>
    #include
    <cstdio>
    using namespace std;

    struct Elem
    {
    int a, b;
    bool operator <=(const Elem &x)
    {
    return this->a * (long long) x.b <= x.a * (long long) this->b;
    }
    bool operator <(const Elem &x)
    {
    return this->a * (long long) x.b < x.a * (long long) this->b;
    }
    Elem
    operator -(const Elem &x)
    {
    Elem ret;
    ret.a
    = this->a * x.b - x.a * this->b;
    ret.b
    = this->b * x.b;
    return ret;
    }
    } s;

    int n, m;

    Elem binarysearch(
    int a)
    {
    Elem ret;
    ret.b
    = a;
    int l = 1;
    int r = 5000;
    while (l < r)
    {
    int mid = (l + r) / 2;
    if (mid * m > n * a)
    r
    = mid;
    else
    l
    = mid + 1;
    }
    ret.a
    = l;
    return ret;
    }

    int main()
    {
    //freopen("t.txt", "r", stdin);
    bool first = true;
    while (scanf("%d%d", &n, &m) != EOF)
    {
    if (first)
    first
    = false;
    else
    putchar(
    '\n');
    s.a
    = 5000;
    s.b
    = 1;
    Elem y;
    y.a
    = n;
    y.b
    = m;
    for (int i = 1; i <= m; i++)
    {
    Elem x
    = binarysearch(i);
    Elem z
    = x;
    z.a
    --;
    if ((x - y) <= (y - z))
    z
    = x;
    Elem temp
    = z - y;
    if (temp.a < 0)
    temp.a
    = -temp.a;
    if (temp < s)
    {
    s
    = temp;
    printf(
    "%d/%d\n", z.a, z.b);
    }
    }
    }
    return 0;
    }
  • 相关阅读:
    Spring4+SpringMVC+MyBatis登录注册详细
    Spring MVC登录注册以及转换json数据
    MyBatis+mysql查询和添加数据
    html5中的选择器
    倒影(转)
    bi包
    函数作用域
    节点开始
    window.onload中失效的问题
    Node.js简介
  • 原文地址:https://www.cnblogs.com/rainydays/p/2181481.html
Copyright © 2011-2022 走看看