题目意思:
给出一个数字x和一个范围,在这个方位内招两个数字,是它们的商最接近x
解:
1、暴力枚举, a = 1, b = 1 开始,
如果 b / a < n ===> ++b; 否则, ++a
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
using namespace std;double n;
int d, a, b;
int x, y;
double min_sub = 110000; //最大的差值
int ans_a, ans_b;int main()
{
scanf("%lf", &n);scanf("%d", &d);a = 1, b = 1;while(a <= d && b <= d){
double tmp = (double)b / (double)a;if(min_sub > abs(tmp - n)){
min_sub = abs(tmp - n);ans_a = a, ans_b = b;}if(tmp < n){
++b; }else{
++a;}}printf("%d %d\n", ans_b, ans_a);return 0;
}/* 3.14159265358979 10000 *//* 355 113 */