poj 1650 Integer Approximation

Integer Approximation

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5326   Accepted: 1750

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

Source

Northeastern Europe 2001, Far-Eastern Subregion

分析:

枚举

 1 #include<iostream>
 2 using namespace std;
 3 int main(){
 4     double a,min=100005;
 5     int n;
 6     cin>>a>>n;
 7     double b,c;
 8     c=b=1;
 9     int p=1,q=1;
10     while(c<=n&&b<=n){
11          if(c/b>a){
12              if(c/b-a<min){
13                  min=c/b-a;
14                  p=c;
15                  q=b;
16              }
17              b++;
18          }
19          else{
20              if(a-c/b<min){
21                  min=a-c/b;
22                  p=c;
23                  q=b;
24              }
25              c++;
26          }
27     }
28     cout<<p<<" "<<q<<endl;
29     return 0;
30 }
时间: 2024-10-16 03:25:42

poj 1650 Integer Approximation的相关文章

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

题目 我要开始练习一些java的简单编程了^v^ import java.io.*; import java.util.*; import java.math.*; public class Main { /** * @param args */ public static void main(String[] args) throws Exception { // 定义并打开输入文件 Scanner cin = new Scanner(System.in); BigInteger a, sum

poj 1503 Integer Inquiry(多个大数相加)

题目链接:http://poj.org/problem?id=1503 Description One of the first users of BIT's new supercomputer was Chip Diller. He extended his exploration of powers of 3 to go from 0 to 333 and he explored taking various sums of those numbers. ``This supercomput

POJ 1053 Integer Inquiry (大数加法,还是Java大法好)

Integer Inquiry Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 32674   Accepted: 12789 Description One of the first users of BIT's new supercomputer was Chip Diller. He extended his exploration of powers of 3 to go from 0 to 333 and he

POJ 1053 Integer Inquiry &amp;&amp; HDOJ 1047 Integer Inquiry (大数加法)

题目链接(POJ):http://poj.org/problem?id=1503 题目链接(HDOJ):http://acm.hdu.edu.cn/showproblem.php?pid=1047 Integer Inquiry Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 30856   Accepted: 12007 Description One of the first users of BIT's new su

poj 1716 Integer Intervals

 Integer Intervals http://poj.org/problem?id=1716 Time Limit: 1000MS   Memory Limit: 10000K       Description An integer interval [a,b], a < b, is a set of all consecutive integers beginning with a and ending with b. Write a program that: finds the m

POJ 1503 Integer Inquiry

http://poj.org/problem?id=1503 题意:给出n个数,计算和. 思路: 基础的大整数加法题. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <cmath> 6 #include<vector> 7 using namespace std; 8 9 const in

POJ 1503 Integer Inquiry 大数 难度:0

题目链接:http://poj.org/problem?id=1503 1 import java.io.*; 2 import java.math.BigInteger; 3 import java.util.Scanner; 4 5 public class Main { 6 public static void main(String []args) throws IOException{ 7 Scanner scanner=new Scanner(System.in); 8 BigInt

POJ - 1716 Integer Intervals(差分约束系统)

题目大意:给出N个区间,要求你找出M个数,这M个数满足在每个区间都至少有两个不同的数 解题思路:还是不太懂差分约束系统,数学不太好 借鉴了别人的思路,感觉有点DP思想 设d[i]表示[0,i-1]这个区间有d[i]个数满足要求 则给定一个区间[a,b],就有d[b + 1] - d[a] >= 2(b + 1是因为b也算在区间内) 将其转换为d[a] - d[b + 1] <= -2,这是第一个式子 接着在区间[i,i+1]中满足 d[i + 1] - d[i] >= 0 转换为 d[i

POJ 1716 Integer Intervals#贪心

(- ̄▽ ̄)-* //求一个集合,这个集合与任意一个区间的交集,需至少有两个数字 //贪心过程:按n个区间的最右值从小到大对区间进行排列, //集合首先取第一个区间的最右两个数字, //到第二个区间,判断集合里的数有没有在区间里 //没有的话,就从第二个区间的最右开始往左取(cnt=0取最后两个数,cnt=1取最后一个数) #include<iostream> #include<cstdio> #include<cstring> #include<algorith