POJ 3039 搜索??? (逼近)
思路:
抄的题解
这叫搜索?
难以理解
我觉得就是枚举+逼近
//By SiriusRen
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n,recx,recy,x,y;
int gcd(int a,int b){
return b?gcd(b,a%b):a;
}
int main(){
while(~scanf("%d%d",&x,&y)){
double temp,a,minn=66666.666;
a=(double)x/y;
int n=x,m=y;
x=y=1;
while(x<=32767&&y<=32767){
temp=(double)x/y;
int p=gcd(x,y),dx=x/p,dy=y/p;
if(abs(temp-a)<abs(minn-a)&&(!(dx==n&&dy==m)))
minn=temp,recx=dx,recy=dy;
if(temp<a)x++;
else y++;
}
printf("%d %d\n",recx,recy);
}
}