You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
977 B

#include <bits/stdc++.h>
using namespace std;
// 1498 902
int main() {
int n, d;
cin >> n >> d; // 最后一个形式是: n/d
// 最后一个数的小数
double x = 1.0 * n / d;
double pre = round(x); // 前一个小数
// 第一个
cout << pre << "/1" << endl;
// 从第二个开始
// 分母
for (int i = 1; i <= d; i++) {
// 分子
for (int j = 1; j <= n; j++) {
double cur = 1.0 * j / i; // 当前枚举到的小数
if (abs(cur - x) >= abs(pre - x)) continue; // 如果当前枚举到的小数与最后小数的距离,大于,上一个小数与最后小数的距离
// 说明,现有枚举到的数更贴近目标,需要打印
cout << j << "/" << i << endl;
pre = cur; // 将上一个小数的距离更新为现在小数的距离,为了下一次循环做准备工作
}
}
return 0;
}