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

  1. 3.14159265358979
  2. 10000

Sample Output

  1. 355 113
    题目大意:给出一个数字x和一个范围,在这个方位内招两个数字,是它们的商最接近x
    解题方法:直接枚举,取a,b1每次对a,b求商,如果a / b > x,则a增加1,否则b增加1,每次记录下差值最小时a,b的值。
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5. double x, a, b, n, Min, n1, n2;
  6. scanf("%lf%lf", &x, &n);
  7. a = ;
  8. b = ;
  9. Min = n + ;
  10. while(a <= n && b <= n)
  11. {
  12. if (a / b > x)
  13. {
  14. if (a / b - x < Min)
  15. {
  16. Min = a / b - x;
  17. n1 = a;
  18. n2 = b;
  19. }
  20. b++;
  21. }
  22. else
  23. {
  24. if (x - a / b < Min)
  25. {
  26. Min = x - a / b;
  27. n1 = a;
  28. n2 = b;
  29. }
  30. a++;
  31. }
  32. }
  33. printf("%.0f %.0f\n", n1, n2);
  34. return ;
  35. }

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. CSS扇形展开效果

    知识点预备: [1]CSS3中特别重要的transform中的rotate(),现在transform可以将元素进行2D和3D变形. 2D transform常用的transform-function ...

  2. Redis安装与日常使用

    下载与安装 $ wget http://download.redis.io/releases/redis-3.0.3.tar.gz $ tar xzf redis-3.0.3.tar.gz $ cd ...

  3. atitit.提升备份文件复制速度(3) ----建立同步删除脚本

    atitit.提升备份文件复制速度(3) ----建立同步删除脚本 1. 建立同步删除脚本两个方法.. 1 2. 1从回收站info2文件... 1 3. 清理结束在后snap比较 1 4. Npp  ...

  4. 更新日志 - fir.im 回归,上线 Android Studio 插件

    上周 fir.im 经历了一场前所未有的挑战,因为自查应用网站暂停,在事情发生4天内我们完成了自查,fir.im 正式回归.煎熬的 98 个小时,感谢开发者与用户对我们的信任和支持. 使用注意: 禁止 ...

  5. Singleton模式和Mono-State模式

    类和实例 对于大多数的类,都可以创建多个实例.在需要和不需要时,创建和删除这些实例.该过程会伴随着内存的分配和归还. 同时,有一些类,应该仅有一个实例.该实例在程序启动/结束时被创建和删除. root ...

  6. Django基础——Model篇(二)

    一 Model连表关系 一对多:models.ForeignKey(其他表)    多对多:models.ManyToManyField(其他表)    一对一:models.OneToOneFiel ...

  7. Leetcode 278 First Bad Version 二分查找(二分下标)

    题意:找到第一个出问题的版本 二分查找,注意 mid = l + (r - l + 1) / 2;因为整数会溢出 // Forward declaration of isBadVersion API. ...

  8. 安装 Autoconf 2.69版

    发生错误configure.ac:8: error: Autoconf version 2.64 or higher is required 1.检查版本 [root@localhost Deskto ...

  9. HTML常用命名和CSS reset代码【收集总结】

    CSS命名规则 头:header 内容:content/containe 尾:footer 导航:nav 侧栏:sidebar 栏目:column 页面外围控制整体布局宽度:wrapper 左右中:l ...

  10. QueryTask查询结果最多500条的问题

    参考: http://dingtao-wgs.blog.163.com/blog/static/5026071420129813059865/ 上面文章中给出的解决方法:今天在做querytask的时 ...