切换到宽版
  • 14650阅读
  • 16回复

『讨论』USACO Ordered Fractions [复制链接]

上一主题 下一主题
离线archimedes

只看该作者 10 发表于: 2006-01-28
我的结果
USACO Training Grader Results for oi fans [oifans1]

TASK: frac1
LANG: PASCAL

Compiling...
Compile: OK

Executing...
sending data frac1
Test 1 OK [0.002 secs]
Test 2 OK [0.002 secs]
Test 3 OK [0.003 secs]
Test 4 OK [0.003 secs]
Test 5 OK [0.002 secs]
Test 6 OK [0.003 secs]
Test 7 OK [0.003 secs]
Test 8 OK [0.005 secs]
Test 9 OK [0.009 secs]
Test 10 OK [0.015 secs]
Test 11 OK [0.037 secs]

All tests OK.
Your program ('frac1') produced all correct answers! This is your
submission #11 for this problem. Congratulations!
[ 此贴被sammy312在2006-01-28 16:31重新编辑 ]
离线stevenjl

只看该作者 11 发表于: 2006-01-31
gcd那段写得漂亮,我怎么就想不到呢?
Dream Walker...
离线stevenjl

只看该作者 12 发表于: 2006-01-31

楼主的代码很好,可读性也很强!
稍微改了改,主要是缩小了数据类型。使用了一个FP的新类型(smallint),提高了一些时间,小修改,再次赞颂楼主!

TASK: frac1
LANG: PASCAL

Compiling...
Compile: OK

Executing...
sending data (frac1) frac1 /home/kolstad/trainweb

Test 1 OK [0.002 secs]
Test 2 OK [0.003 secs]
Test 3 OK [0.002 secs]
Test 4 OK [0.003 secs]
Test 5 OK [0.003 secs]
Test 6 OK [0.003 secs]
Test 7 OK [0.003 secs]
Test 8 OK [0.005 secs]
Test 9 OK [0.009 secs]
Test 10 OK [0.011 secs]
Test 11 OK [0.032 secs]

All tests OK.

Your program ('frac1') produced all correct answers!  This is your
submission #41 for this problem.  Congratulations!

Dream Walker...
离线archimedes

只看该作者 13 发表于: 2006-02-01
不敢当,说实话,我喜欢用object类型
tctc的方法也是可以的。
离线archimedes

只看该作者 14 发表于: 2006-02-01
说实话,gcd那一段是从我们论坛[最新动态]文章“备战NOIP2005必须掌握的基本算法”那里抄的gcd。不是自己写的~
离线pzy
只看该作者 15 发表于: 2006-02-13
{
PROG:frac1
LANG:PASCAL
}
const
  inf='frac1.in';
  outf='frac1.out';
type
  pzy=record
    a,b:integer;
  end;
var
  n,k,i,j:integer;
  x:array [1..maxint] of pzy;
  temp:pzy;
function gy(a,b:integer):integer;
  var
    r:integer;
  begin
    r:=a mod b;
    while r<>0 do
      begin
        a:=b;
        b:=r;
        r:=a mod b;
      end;
    gy:=b;
  end;
begin
  assign(input,inf);
  reset(input);
  assign(output,outf);
  rewrite(output);
  readln(n);
  x[1].a:=0;
  x[1].b:=1;
  x[2].a:=1;
  x[2].b:=1;
  k:=2;
  for i:=2 to n do
    for j:=1 to i-1 do
      if gy(i,j)=1 then
        begin
          inc(k);
          x[k].a:=j;
          x[k].b:=i;
        end;
  for i:=1 to k-1 do
    for j:=i+1 to k do
      if x.a*x[j].b>x.b*x[j].a then
        begin
          temp:=x;
          x:=x[j];
          x[j]:=temp;
        end;
  for i:=1 to k do
    writeln(x.a,'/',x.b);
  close(input);
  close(output);
end.
离线pzy
只看该作者 16 发表于: 2006-02-13
TASK: frac1
LANG: PASCAL

Compiling...
Compile: OK

Executing...
sending data (frac1) frac1 /home/kolstad/trainweb
Test 1 OK [0.003 secs]
Test 2 OK [0.003 secs]
Test 3 OK [0.002 secs]
Test 4 OK [0.003 secs]
Test 5 OK [0.003 secs]
Test 6 OK [0.003 secs]
Test 7 OK [0.003 secs]
Test 8 OK [0.005 secs]
Test 9 OK [0.008 secs]
Test 10 OK [0.011 secs]
Test 11 OK [0.029 secs]

All tests OK.




{
PROG:frac1
LANG:PASCAL
}
const
  inf='frac1.in';
  outf='frac1.out';
type
  pzy=record
    a,b:integer;
    c:real;
  end;
var
  n,k,i,j:integer;
  x:array [1..maxint] of pzy;
  temp:pzy;
function gy(a,b:integer):integer;
  var
    r:integer;
  begin
    r:=a mod b;
    while r<>0 do
      begin
        a:=b;
        b:=r;
        r:=a mod b;
      end;
    gy:=b;
  end;
procedure quicksort(v1,v2:integer);
  var
    i,j:integer;
    mid:real;
    temp:pzy;
  begin
    mid:=x[(v1+v2) div 2].c;
    i:=v1;
    j:=v2;
    while i<j do
      begin
        while x.c<mid do
          inc(i);
        while x[j].c>mid do
          dec(j);
        if i<=j then
          begin
            temp:=x;
            x:=x[j];
            x[j]:=temp;
            inc(i);
            dec(j);
          end;
      end;
    if i<v2 then
      quicksort(i,v2);
    if j>v1 then
      quicksort(v1,j);
  end;
begin
  assign(input,inf);
  reset(input);
  assign(output,outf);
  rewrite(output);
  readln(n);
  x[1].a:=0;
  x[1].b:=1;
  x[1].c:=0;
  x[2].a:=1;
  x[2].b:=1;
  x[2].c:=1;
  k:=2;
  for i:=2 to n do
    for j:=1 to i-1 do
      if gy(i,j)=1 then
        begin
          inc(k);
          x[k].a:=j;
          x[k].b:=i;
          x[k].c:=j/i;
        end;
  quicksort(1,k);
  for i:=1 to k do
    writeln(x.a,'/',x.b);
  close(input);
  close(output);
end.
[ 此贴被pzy在2006-02-13 19:48重新编辑 ]
快速回复
限100 字节
 
上一个 下一个