http://acm.hdu.edu.cn/showproblem.php?pid=1239
一道水题,只要遍历找出所有的素数,应为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;
}