问题 III

languagec 2004-04-20 10:18:32
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 file 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).

Process to the end of file.


Output

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


Sample Input

3.14159265358979
10000


Sample Output

355 113






因为A要和N/D最接近,不防设A=N/D,既N=D*A,因为N小于D*A,所以从N开始一直N++,总能找到一个(D一定是)与A相差最小的N/D. 因为|A-N/D|的值必然会先随N的增加先减小后增大.我们求出误差最小的N.
依次求出在D的范围内的N/D,去误差最小的N,D.
不知道算法分析有没有错,我怎么交都没对.


#include <iostream.h>
float abs(float num)
{
return num>0?num:-num;
}


float F(long &N,long D,float A)
{
float t=abs((float)N/D-A);
while(1)
{
if(abs((float)(N+1)/D-A)<t)
{
t=abs((float)++N/D-A);
}
else return t;
}
}



int main()
{
float A,t,T;
long N,D,NN,DD,d;
while(cin>>A>>D)
{
T=D;
for(d=1;d<=D;d++)
{
N=A*d;
t=F(N,d,A);
if(t<T)
{
T=t;
NN=N;
DD=d;
}
}
cout<<NN<<" "<<DD<<endl;
}
return 0;
}
...全文
39 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
languagec 2004-04-27
  • 打赏
  • 举报
回复
if(n>(m-n))
n=m-n;
加上这句就行了
languagec 2004-04-27
  • 打赏
  • 举报
回复
星星的点评一针见血!
shines77 2004-04-27
  • 打赏
  • 举报
回复
像你前面那样把分子的每一位乘数保存在一个数组里,然后自己写一个大数的乘法就可以了
mmmcd 2004-04-27
  • 打赏
  • 举报
回复
http://acm.zju.edu.cn/forum/viewtopic.php?t=2036&highlight=1601
http://acm.zju.edu.cn/forum/viewtopic.php?t=1358&highlight=1601
gnefuil 2004-04-26
  • 打赏
  • 举报
回复
the size of arry is just 250.....
languagec 2004-04-26
  • 打赏
  • 举报
回复
It can't do so.
gnefuil 2004-04-26
  • 打赏
  • 举报
回复
估计是数组超界吧
languagec 2004-04-26
  • 打赏
  • 举报
回复
run time error
languagec 2004-04-22
  • 打赏
  • 举报
回复
可能是算法错了,我再想想.
shines77 2004-04-22
  • 打赏
  • 举报
回复
有点问题,上面T = (long)((double)L / A); 应该再加1
T = (long)((double)L / A) + 1;
shines77 2004-04-22
  • 打赏
  • 举报
回复
的确你没有考虑L < A的情况,

例如:
A = 9.172893712
L = 5

输出应该是:
5 1
shines77 2004-04-22
  • 打赏
  • 举报
回复
languagec(各有所求):
你要自己回复过才能在我参与的帖子里看到,否则只能从我的问题里找,

你的问题在于:
float t=abs((float)N/D-A);

应该写这样:
float t=abs((float)N/(float)D-A);

其他地方类似,
有个地方可以改进一下,D有个最大值,超过这个值就不必往下计算到L了,例如:

T = (long)((double)L / A);

for(d=1;d<=D;d++) 改为 for(d=1;d<=(long)((float)D / A);d++)
languagec 2004-04-21
  • 打赏
  • 举报
回复
http://acm.zju.edu.cn/show_problem.php?pid=1601
languagec 2004-04-21
  • 打赏
  • 举报
回复
大家能看到这个问题吗? 和昨天那个一起发的.为什么我在列表里看不到?
大家能看到吗?
LeeMaRS 2004-04-21
  • 打赏
  • 举报
回复
又是ZOJ的题呢。你没有考虑L < A的情况。另外你的程序似乎也有点问题,浮点数的应该是fabs吧?
gcz5212314 2004-04-21
  • 打赏
  • 举报
回复
关注

33,006

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧