求k范围内的n的最大因数,注意这题不能用sqrt(k),这样会有误差,比如 n=16,k=4这个就过不了,必须用sqrt(n),然后判断因数是否在k的范围内就行。
AC代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
int main() {
int t;
cin>>t;
while(t--){
int n,k;
cin>>n>>k;
int ans=0;
for(int i=1;i<=sqrt(n);i++){
if(n%i==0){
//ans=max(ans,i);
//if(n/i<=k)ans=max(ans,n/i);
if(i<=k)ans=max(ans,i);
if(n/i<=k)ans=max(ans,n/i);
}
}
cout<<n/ans<<endl;
}
}