答案来了。
#include <bits/stdc++.h>
using namespace std;
int A,B,L;
bool cop(int a,int b){
for(int d=2;d<=a;d++){
if(a%d==0&&b%d==0){
return false;
}
}
return true;
}
int main(){
scanf("%d%d%d",&A,&B,&L);
double ans=100000000;
int ansA=-1;
int ansB=-1;
for(int a=1;a<=L;a++){
for(int b=1;b<=L;b++){
if(cop(a,b)&&(double)a/b>=(double)A/B){
double tmp=1.0*a/b-1.0*A/B;
if(tmp<ans){
ans=tmp;
ansA=a;
ansB=b;
}
}
}
}
printf("%d %d",ansA,ansB);
}
免费的:
#include <bits/stdc++.h>
using namespace std;
int A,B,L;
bool cop(int a,int b){
for(int d=2;d<=a;d++){
if(a%d==0&&b%d==0){
return false;
}
}
return true;
}
int main(){
scanf("%d%d%d",&A,&B,&L);
double ans=100000000;
int ansA=-1;
int ansB=-1;
for(int a=1;a<=L;a++){
for(int b=1;b<=L;b++){
if(cop(a,b)&&(double)a/b>=(double)A/B){
double tmp=1.0*a/b-1.0*A/B;
if(tmp<ans){
ans=tmp;
ansA=a;
ansB=b;
}
}
}
}
printf("%d %d",ansA,ansB);
}