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
Source
cwj
题解:
把所有的分数枚举出来,排序,去重,输出······
代码:
var
n,i,j,l:longint;
z,m:array[1..10000] of longint;
a:array[1..10000] of real;
function gcd(a,b:longint):longint;
var
r:longint;
begin
while b<>0 do
begin
r:=a mod b;
a:=b;b:=r;
end;
exit(a);
end;
procedure qsort(l,r:longint);
var
i,j,c:longint;
k,t:real;
begin
if l>=r then exit;
i:=l;j:=r;
k:=a[l+random(r-l+1)];
repeat
while (a[i]<k) do inc(i);
while (a[j]>k) do dec(j);
if i<=j then
begin
t:=a[i];a[i]:=a[j];a[j]:=t;
c:=z[i];z[i]:=z[j];z[j]:=c;
c:=m[i];m[i]:=m[j];m[j]:=c;
inc(i);dec(j);
end;
until i>j;
qsort(l,j);
qsort(i,r);
end;
begin
readln(n);
l:=1;
m[1]:=1;
for i:=1 to n do
for j:=i to n do
if gcd(i,j)=1 then
begin
inc(l);
z[l]:=i;
m[l]:=j;
end;
randomize;
for i:=1 to l do
a[i]:=z[i]/m[i];
qsort(1,l);
for i:=1 to l do
writeln(z[i],'/',m[i]);
end.