C语言模拟考|Proper Fraction

来源:互联网 发布:c语言编写驱动程序 编辑:程序博客网 时间:2024/05/16 18:16

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/21/32/31/41/52/53/51/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);}}}}


1 0
原创粉丝点击