Integer Approximation
https://vjudge.net/problem/POJ-1650
直接暴力枚举每个数,得出最接近的两个整数
#include<stdio.h> #include<iostream> #include<map> #include<string.h> #include<vector> #include<math.h> using namespace std; //1 <= L <= 100000 int main() { double num; double maxn; while(~scanf("%lf%lf",&num,&maxn)) { double left=1; double right=1; double minn=9999999999; double mina=0; double minb=0; while(left<=maxn&&right<=maxn) { if(left/right>num) { if(fabs(left/right-num)<minn) { minn=fabs(left/right-num); mina=left; minb=right; // cout<<minn<<" "<<mina<<" "<<minb<<endl; } right++; } else { if(fabs(left/right-num)<minn) { minn=fabs(left/right-num); mina=left; minb=right; // cout<<minn<<" "<<mina<<" "<<minb<<endl; } left++; } // cout<<left<<" "<<right<<endl; } cout<<mina<<" "<<minb<<endl; } return 0; }