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 = ;
b = ;
Min = n + ;
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\n", n1, n2);
return ;
}

POJ 1650 Integer Approximation的更多相关文章

  1. Poj 1503 Integer Inquiry

    1.链接地址: http://poj.org/problem?id=1503 2.题目: Integer Inquiry Time Limit: 1000MS   Memory Limit: 1000 ...

  2. poj 1716 Integer Intervals (差分约束 或 贪心)

    Integer Intervals Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12192   Accepted: 514 ...

  3. POJ 1503 Integer Inquiry 大数 难度:0

    题目链接:http://poj.org/problem?id=1503 import java.io.*; import java.math.BigInteger; import java.util. ...

  4. POJ 1716 Integer Intervals 差分约束

    题目:http://poj.org/problem?id=1716 #include <stdio.h> #include <string.h> #include <ve ...

  5. poj 1503 Integer Inquiry (高精度运算)

    题目链接:http://poj.org/problem?id=1503 思路分析: 基本的高精度问题,使用字符数组存储然后处理即可. 代码如下: #include <iostream> # ...

  6. POJ 1503 Integer Inquiry(大数相加)

    一.Description One of the first users of BIT's new supercomputer was Chip Diller. He extended his exp ...

  7. POJ 1201 Intervals || POJ 1716 Integer Intervals 差分约束

    POJ 1201 http://poj.org/problem?id=1201 题目大意: 有一个序列,题目用n个整数组合 [ai,bi,ci]来描述它,[ai,bi,ci]表示在该序列中处于[ai, ...

  8. poj 1716 Integer Intervals(差分约束)

    1716 -- Integer Intervals 跟之前个人赛的一道二分加差分约束差不多,也是求满足条件的最小值. 题意是,给出若干区间,需要找出最少的元素个数,使得每个区间至少包含两个这里的元素. ...

  9. POJ 1503 Integer Inquiry(大数相加,java)

    题目 我要开始练习一些java的简单编程了^v^ import java.io.*; import java.util.*; import java.math.*; public class Main ...

随机推荐

  1. SVG的路径动画效果

    使用SVG animateMotion实现的一个动画路径效果,相关代码如下. 在线调试唯一地址:http://www.gbtags.com/gb/debug/c88f4099-5056-4ad7-af ...

  2. Maven系列--"maven-compiler-plugin"的使用

    maven是个项目管理工具,如果我们不告诉它我们的代码要使用什么样的jdk版本编译的话,它就会用maven-compiler-plugin默认的jdk版本来进行处理,这样就容易出现版本不匹配的问题,以 ...

  3. spring 配置定时任务

    spring的定时任务配置分为三个步骤:1.定义任务2.任务执行策略配置3.启动任务1.定义任务 <!--要定时执行的方法--> <bean id="testTaskJob ...

  4. 虚拟机下samba简单安装配置

    系统是Win7 虚拟机是CenterOS6.5 1.关闭防火墙以及关闭SELINUX的强制模式(重要): service iptables stop//关闭防火墙 setenforce 0 //关闭S ...

  5. JavaScript的for循环编写九九乘法表

    for(var i = 1; i <= 10; i++) { for(var j = 1; j <= i; j++) { document.writeln(i + '*' + j + '= ...

  6. js/jq宽高的理解与运用

    document:1. 与client相关的宽高document.body.clientWidthdocument.body.clientHeightdocument.body.clientLeftd ...

  7. LDR 和 ADR 彻底详解

    0.什么是位指令? 答:伪指令(Pseudo instruction)是用于告诉汇编程序如何进行汇编的指令.它既不控制机器的操作也不被汇编成机器代码, 只能为汇编程序所识别并指导汇编如何进行. 1.L ...

  8. 无线电源传输 Wireless Power Consortium (WPC) Communication

    Universally Compatible Wireless Power Using the Qi Protocol Wireless charging of portable electronic ...

  9. Cocos2dx.3x入门三部曲-Hello Game项目创建(二)

    一.前提: 完成cocos2d-x-3.x开发环境的安装配置. 具体参考:Cocos2d-x.3x_软件配置篇 二.本篇目标: l  创建一个名为hello game的cocos2dx 3.3的项目 ...

  10. 上海邮政EMS海关清关(个人) 流程

    最近雾埋越来越严重,上个星期买了一个tacx骑行台,不料运气欠佳,被税了.那就去乖乖缴税吧. 拿着EMS的通知单(没有通知单就不要去了),到通知单指定的地址(上海有两处,我的是武定路458号)清关提货 ...