您的位置:首页 > 其它

POJ 1650

2015-06-10 16:35 232 查看
#include <iostream>
#include <cmath>  //wo de 编译器里的这个abs的功能不能用啊!
using namespace std;
int main()
{
double a;
double per;//误差计数器;
int son_num;//代表分子的枚举;
int    length;//范围;
int mother_num;//分母的枚举;
int s;//保存当前误差最小的那个分子;
int m;//保存当前误差最小的那组分母;
double item;//保存分母的浮点型
cin>>a;
cin>>length;
per=length;//第一次把误差设置的很大;
for(son_num = 1;son_num <= length;son_num++)
{
if (per < 1E-14)//忽略精度;因为要求的精度已经达到了,所以以后就不必在改变 已经算好的分子和分母了!
per = 0;
item = a*son_num;//
mother_num = int(item+0.5);//四舍五入。把最接近的那个整数找出来!
if (mother_num == 0)//临界状态
mother_num = 1;
if (mother_num > length)//
mother_num = length;
if (per > abs(a-(double)mother_num/double(son_num)))//始终保存的是最小误差的那组;
{
per = abs(a-(double)mother_num/double(son_num));
m = mother_num;
s = son_num;
}
}
cout<<m<<' '<<s<<endl;//
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: