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;// }
相关文章推荐
- 如何定义和区分高级软件开发工程师
- camera预览是闪退的问题
- Mybatis学习(二)Mybatis框架的原理
- Eclipse搜索快捷键
- android退出整个应用程序
- hadoop集群搭建过程中几个配置参数
- 【转】博客园的创始人-dudu今天才发现的哦
- POJ 1631
- POJ 1579
- Xcode自动注释插件: VVDocumenter使用和安装
- swing组件JTabel和JTree使用总结
- 设计模式之 - 模板模式
- js判断是否在iframe中;解决F5刷新浏览器时,iframe都会变成默认页面,而不是当前页面
- hibernate参数配置大全
- 使用canvas画图
- 详细解析BluetoothAdapter的详细api
- JQuery
- POJ 1607
- POJ 1573
- 下决心不犹豫