题目:点击打开链接
题意:给个小数p(0<=p<1),求差值与其最小的不可约分分数且分母不能超过n。
分析:二分法里数列,可以看成树的遍历。
![](https://img-blog.csdn.net/20180501211257454?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzM3MTA5MzI5/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
代码:
#pragma comment(linker, "/STACK:102400000,102400000")///手动扩栈
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cassert>
#include<string>
#include<cstdio>
#include<bitset>
#include<vector>
#include<cmath>
#include<ctime>
#include<stack>
#include<queue>
#include<deque>
#include<list>
#include<set>
#include<map>
using namespace std;
#define debug test
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define inf 0x3f3f3f3f
#define eps 1e-10
#define PI acos(-1.0)
typedef pair<int,int> PII;
const ll mod = 1e9+7;
const int N = 1e6+10;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qp(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
int to[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int t,k,m;
double x;
int main() {
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>t;
while(t--) {
cin>>k>>m>>x;
int a=0,b=1,c=1,d=1,e,f;
while(1) {
e=a+c,f=b+d;
int gd=__gcd(e,f);
e/=gd,f/=gd;
if(f>m) break;
if(1.0*e/f<=x) a=e,b=f;
else c=e,d=f;
}
cout<<k<<" ";
if(fabs(1.0*a/b-x)<fabs(1.0*c/d-x)) cout<<a<<"/"<<b<<endl;
else cout<<c<<"/"<<d<<endl;
}
return 0;
}