zoukankan      html  css  js  c++  java
  • POJ 1650 Integer Approximation

    Integer Approximation
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 5081   Accepted: 1652

    Description

    The FORTH programming language does not support floating-point arithmetic at all. Its author, Chuck Moore, maintains that floating-point calculations are too slow and most of the time can be emulated by integers with proper scaling. For example, to calculate the area of the circle with the radius R he suggests to use formula like R * R * 355 / 113, which is in fact surprisingly accurate. The value of 355 / 113 ≈ 3.141593 is approximating the value of PI with the absolute error of only about 2*10-7. You are to find the best integer approximation of a given floating-point number A within a given integer limit L. That is, to find such two integers N and D (1 <= N, D <= L) that the value of absolute error |A - N / D| is minimal.

    Input

    The first line of input contains a floating-point number A (0.1 <= A < 10) with the precision of up to 15 decimal digits. The second line contains the integer limit L. (1 <= L <= 100000).

    Output

    Output file must contain two integers, N and D, separated by space.

    Sample Input

    3.14159265358979
    10000
    

    Sample Output

    355 113
    题目大意:给出一个数字x和一个范围,在这个方位内招两个数字,是它们的商最接近x。
    解题方法:直接枚举,取a,b为1每次对a,b求商,如果a / b > x,则a增加1,否则b增加1,每次记录下差值最小时a,b的值。
    #include <stdio.h>
    
    int main()
    {
        double x, a, b, n, Min, n1, n2;
        scanf("%lf%lf", &x, &n);
        a = 1;
        b = 1;
        Min = n + 1;
        while(a <= n && b <= n)
        {
            if (a / b > x)
            {
                if (a / b - x < Min)
                {
                    Min = a / b - x;
                    n1 = a;
                    n2 = b;
                }
                b++;
            }
            else
            {
                if (x - a / b < Min)
                {
                    Min = x - a / b;
                    n1 = a;
                    n2 = b;
                }
                a++;
            }
        }
        printf("%.0f %.0f
    ", n1, n2);
        return 0;
    }
  • 相关阅读:
    一本通1273货币系统(方案数背包)
    背包体积循环正序和逆序的区别
    Python字典的底层原理和优缺点
    Linux各目录及每个目录的详细介绍
    openwrt 下python程序后台运行,并将打印信息保存文件
    pycharm同一目录下无法import其他文件
    python sqlite3学习笔记
    python sqlite3查询表记录
    Pycharm快捷键的使用
    Python3 Address already in use 解决方法
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3256085.html
Copyright © 2011-2022 走看看