hdu 1239 Calling Extraterrestrial Intelligence Again
2016-02-26 14:11
615 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1239
一道水题,只要遍历找出所有的素数,应为a,b都不大,所以直接穷举法就可以过了。
一道水题,只要遍历找出所有的素数,应为a,b都不大,所以直接穷举法就可以过了。
#include<iostream> using namespace std; int m, a, b,l; int p[999],_p,_q; int isP(int n){ for (int i = 2; i*i <= n; i++){ if (n%i == 0) return 0; } return 1; } void init(){ l = 0; for (int i = 2; i <= 999; i++){ if (isP(i)) p[l++] = i; } } int main(){ init(); while (cin >> m >> a >> b) { if (!a&&!a&&!b) break; _q = _p = 0; double a_b = (double)a / b; for (int i = 0; i <= l; i++){ for (int j = i; j <= l; j++){ if (p[i] * p[j] > m){ break; } double p_q = (double)p[i] / p[j]; if (a_b <= p_q&&p_q <= 1){ if (p[i] * p[j] >= _p*_q){ _p = p[i]; _q = p[j]; } } } } cout << _p<<" " << _q << endl; } return 0; }
相关文章推荐
- 文件遍历排序函数
- Lua 学习笔记之C API 遍历 Table实现代码
- C#遍历文件夹后上传文件夹中所有文件错误案例分析
- C#中遍历Hashtable的4种方法
- Erlang中遍历取出某个位置的最大值代码
- C++实现图的邻接矩阵存储和广度、深度优先遍历实例分析
- C++实现图的邻接表存储和广度优先遍历实例分析
- C++非递归队列实现二叉树的广度优先遍历
- php遍历目录方法小结
- 一个目录遍历函数
- php遍历删除整个目录及文件的方法
- PHP遍历文件夹与文件类及处理类用法实例
- PHP遍历XML文档所有节点的方法
- php中使用key,value,current,next和prev函数遍历数组的方法
- C#使用前序遍历、中序遍历和后序遍历打印二叉树的方法
- C#使用foreach遍历哈希表(hashtable)的方法
- php递归遍历多维数组的方法
- C#使用yield关键字让自定义集合实现foreach遍历的方法
- C#使用foreach语句简单遍历数组的方法
- JSON遍历方式实例总结