Description
giving an integer N (2<=N<=20) and a real number M (0<M<=1), output all proper fractions whose numerator is less than N ,and denominator is equal to or less than N, and value is equal to or less than M.
Input
an integer N, a real number M in the type of double, in one line seperated by blank space.
Output
proper fractions, each occupies a line. The proper fractions with smaller denominators are in the front. Between two different fractions with same denominator, the one whose numerator is smaller comes first. There is a '\n' after
the final fraction number.
Sample Input
6 0.7
Sample Output
1/2
1/3
2/3
1/4
1/5
2/5
3/5
1/6
#include <stdio.h>
int gcd(int x, int y);
int gcd(int x, int y) {
while (x != y) {
if (x > y) {
x = x - y;
}
else {
y = y - x;
}
}
return x;
}
int main() {
int n, i, j;
double m;
scanf("%d %lf",&n,&m);
for (i = 1; i <= n; ++ i) {
for (j = 1; j <= i; ++ j) {
if (j*1.0/i > m) break;
if (gcd(j, i) == 1) {
printf("%d/%d\n",j,i);
}
}
}
}