您的位置:首页 > 其它

usaco 2.1 frac1 2008.5.10

2016-02-05 11:09 337 查看
usaco 2.1 frac1 2008.5.10

{

ID:

PROG: frac1

LANG: PASCAL

}

program p_frac1;

const fin='frac1.in';fout='frac1.out';

type

arr1=array[1..8000,1..2]of longint;

arr2=array[1..8000]of real;

var i,j,k,p,w,n:longint;

a:arr1;b:arr2;

f1,f2:text;

procedure qsort(i,j:longint);

var

l,r,g:longint;x,y:real;

begin

l:=i;r:=j;

x:=b[(l+r) div 2];

repeat

while b[l]<x do inc(l);

while b[r]>x do dec(r);

ifl<=r then

begin

y:=b[l];b[l]:=b[r];b[r]:=y;

g:=a[l,1];a[l,1]:=a[r,1];a[r,1]:=g;

g:=a[l,2];a[l,2]:=a[r,2];a[r,2]:=g;

inc(l);dec(r);

end;

until l>r;

if r>i then qsort(i,r);

if l<j then qsort(l,j);

end;

function gcd(a,b:integer):integer;

begin

ifb=0 then gcd:=a

else gcd:=gcd (b,a mod b);

end ;

{------------------------------}

begin

assign(f1,fin);reset(f1);

assign(f2,fout);rewrite(f2);

read(f1,n);

writeln(f2,'0/1');

p:=0;

fori:=2 to n do

begin

inc(p);b[p]:=1/i;a[p,1]:=1;a[p,2]:=i;

for j:=2 to i-1 do

if gcd(i,j)=1 then

begin inc(p);b[p]:=j/i;

a[p,1]:=j;a[p,2]:=i;

end;

end;

qsort(1,p);

for i:=1 to p do

writeln(f2,a[i,1],'/',a[i,2]);

writeln(f2,'1/1');

close(f1);close(f2);

end.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: