Description
输入一个自然数N
请写一个程序来增序输出分母小于等于N的最简真分数
Input
单独的一行 一个自然数N(1..160)
Output
每个分数单独占一行
最后一行有回车
Sample Input
5
Sample Output
0/1
1/5
1/4
1/3
2/5
1/2
3/5
2/3
3/4
4/5
1/1
这题用递归和二分做。
每一次递归将分母和和分子和求出来,如果分子和大于分母和就退出。
不然,就继续递归。
代码如下:
var n:longint;
procedure found(x1,y1,x2,y2:longint);
var mx,my:longint;
begin
mx:=x1+x2;
my:=y1+y2;
if my>n then exit;
found(x1,y1,mx,my);
writeln(mx,'/',my);
found(mx,my,x2,y2);
end;
begin
readln(n);
writeln('0/1');
found(0,1,1,1);
writeln('1/1');
end.