题解

70 条题解

  • -1
    @ 2009-11-08 09:55:46

    不能输出x/1 ,出题目不用这样吧,太不讲道理了。而且题目中说是连分数和分数的互化,这样写本质上没错啊!!!

  • -1
    @ 2009-11-08 09:36:22

    小鸟直接模拟居然写了这么长的程序。。。

    program p1;

    var

    ch:char;

    procedure swap(var a,b:longint);

    var

    k:longint;

    begin

    k:=a;

    a:=b;

    b:=k;

    end;

    procedure chuli1;

    var

    a:array[1..200]of longint;

    z,m,i,j,k:longint;

    begin

    fillchar(a,sizeof(a),0);

    read(ch);

    i:=0;

    while ch']' do

    begin

    inc(i);

    while (ch',' )and ( ch';') and (ch']') and (ch'[') do

    begin a[i]:=a[i]*10+(ord(ch)-ord('0')); read(ch); end;

    if ch=']' then break;

    read(ch);

    end;

    readln;

    m:=a[i]; z:=1;

    while i>1 do

    begin

    dec(i);

    z:=z +m *a[i];

    swap(m,z);

    end;

    swap(m,z);

    if m>z then j:=trunc(sqrt(m)) else j:=trunc(sqrt(z));

    for k:=2 to j do

    if (m mod k =0) and(z mod k =0) then

    while (m mod k =0) and(z mod k =0) do

    begin

    m:=m div k;

    z:=z div k;

    end;

    if m1 then

    writeln(z,'/',m) else writeln(z);

    end;

    procedure chuli2;

    var

    s,z,m,i,j,k:longint;

    begin

    z:=0;

    while ch'/' do

    begin

    z:=z*10+(ord(ch)-ord('0'));

    read(ch);

    end;

    read(m);

    readln;

    s:=z div m;

    write('[',s,';');

    z:=z-m*s;

    swap(m,z);

    if(m mod z0) then

    begin

    s:=z div m;

    write(s);

    z:=z-m*s;

    swap(m,z);

    end;

    while (m 0) do

    begin

    s:=z div m;

    write(',',s);

    z:=z-m*s;

    swap(m,z);

    end;

    writeln(']');

    end;

    begin

    while not eof do

    begin

    read(ch);

    if ch='[' then chuli1 else chuli2;

    end;

    end.

  • -1
    @ 2009-11-08 09:30:34

    var

    s:string;

    function gd(m,n:longint):longint;

    begin

    if n=0 then

    gd:=m

    else

    gd:=gd(n,m mod n);

    end;

    procedure a;

    var

    i,tot,fz,fm,tmp,z,code,k:longint;

    x:array[1..1000] of longint;

    begin

    k:=pos(';',s);

    if k=0 then

    begin

    writeln(copy(s,2,length(s)-2));

    exit;

    end;

    if s[k+1]=']' then

    begin

    writeln(copy(s,2,k-2));

    exit;

    end;

    val(copy(s,2,k-2),z,code);

    delete(s,1,k);

    delete(s,length(s),1);

    k:=1;

    tot:=0;

    while true do

    begin

    inc(tot);

    k:=pos(',',s);

    if k0 then

    begin

    val(copy(s,1,k-1),tmp,code);

    x[tot]:=tmp;

    delete(s,1,k);

    end

    else

    begin

    val(s,tmp,code);

    x[tot]:=tmp;

    break;

    end;

    end;

    i:=tot;

    fm:=x[tot];

    fz:=1;

    if i1 then

    repeat

    fz:=fm*x+fz;

    tmp:=fz;

    fz:=fm;

    fm:=tmp;

    dec(i);

    until i=1;

    fz:=fz+fm*z;

    tmp:=gd(fz,fm);

    fz:=fz div tmp;

    fm:=fm div tmp;

    if fm=1 then

    writeln(fz)

    else

    writeln(fz,'/',fm);

    end;

    procedure b;

    var

    i,tot,fz,fm,tmp,z,zz,code,k:longint;

    x:array[1..1000] of longint;

    begin

    k:=pos('/',s);

    if k=0 then

    begin

    writeln(s);

    exit;

    end;

    val(copy(s,1,k-1),fz,code);

    val(copy(s,k+1,length(s)-k),fm,code);

    tmp:=gd(fz,fm);

    fz:=fz div tmp;

    fm:=fm div tmp;

    z:=0;

    while fz>=fm do

    begin

    fz:=fz-fm;

    inc(z);

    end;

    if fz=0 then

    begin

    write('[',z,']');

    exit;

    end;

    write('[',z,';');

    zz:=0;

    repeat

    tmp:=fm;

    fm:=fz;

    fz:=tmp;

    zz:=0;

    while fz>fm do

    begin

    fz:=fz-fm;

    inc(zz);

    end;

    write(zz,',');

    until fz=1;

    write(fm,']');

    writeln;

    end;

    begin

    while not eof do

    begin

    readln(s);

    if s[1]='[' then

    a

    else

    b;

    end;

    end.

    测一下[3;3]就知道了……

  • -1
    @ 2009-11-08 09:27:05

    在昨天的程序里

    加一个补丁

    就行了

    hint:昨天80分……

    编译通过...

    ├ 测试数据 01:答案正确... 0ms

    ├ 测试数据 02:答案正确... 0ms

    ├ 测试数据 03:答案正确... 0ms

    ├ 测试数据 04:答案正确... 0ms

    ├ 测试数据 05:答案正确... 0ms

    ---|---|---|---|---|---|---|---|-

    Accepted 有效得分:100 有效耗时:0ms

    var

    s:string; b:boolean;

    i,j,k,n,i1,i2,x,y,z1,z2:longint;

    a:array[0..10000]of longint;

    function gcd(x,y:longint):longint;

    begin

    if y=0 then exit(x);

    gcd:=gcd(y,x mod y);

    end;

    begin

    while not(eof)do

    begin

    readln(s); b:=false;

    if pos('[',s)0 then

    if pos(';',s)=0 then writeln(copy(s,2,length(s)-2))

    else

    begin fillchar(a,sizeof(a),0);

    i1:=pos(';',s);

    if s[2]='-' then begin b:=true;val(copy(s,3,i1-3),a[0]);end

    else val(copy(s,2,i1-2),a[0]);

    delete(s,1,pos(';',s));i2:=0;

    while pos(',',s)0 do

    begin

    inc(i2);

    val(copy(s,1,pos(',',s)-1),a[i2]);

    delete(s,1,pos(',',s));

    end;

    inc(i2);val(copy(s,1,length(s)-1),a[i2]);

    x:=1;y:=a[i2];

    for j:=i2-1 downto 0 do

    begin

    z1:=a[j]*y+x;

    x:=y;

    y:=z1;

    end;

    if b then writeln(y-2*x*a[0],'/',x)else

    writeln(y,'/',x);

    end

    else if pos('/',s)0then

    if pos('/1',s)=length(s)-1 then writeln('[',copy(s,1,length(s)-2),']')

    else begin

    i1:=pos('/',s); if pos('-',s)0then begin val(copy(s,2,i1-2),x); b:=true;end

    else begin val(copy(s,1,i1-1),x);end;

    val(copy(s,i1+1,length(s)-i1),y);

    z1:=gcd(x,y); write('[');

    x:=x div z1;y:=y div z1;

    if b then begin

    write('-',x div y+1,';');

    x:=y*(x div y+1)-x;end else

    write(x div y,';'); x:=x mod y;

    while (y0)and(y1) do

    begin

    if (y mod x0)then

    write(y div x,',')else write(y div x);

    y:=y mod x;

    z2:=y;y:=x;x:=z2;

    end;

    writeln(']');

    end;

    end;

    end.

    话说这个题还真让我迷茫呢

  • -1
    @ 2009-11-08 09:19:44

    program test1;

    var s:string;

    t1,t,t2,p,code,ls,up,down:longint;

    begin

    while not eof do

    begin

    readln(s);

    if s[1]='[' then begin

    ls:=length(s);

    dec(ls);

    p:=ls;

    while s[p] in ['0'..'9'] do dec(p);

    val(copy(s,p+1,ls-p),t1,code);

    up:=1;down:=t1;

    ls:=p;

    if pos(';',s)=0 then begin writeln(t1);continue;end;

    repeat

    p:=ls-1;

    while s[p] in ['0'..'9'] do dec(p);

    val(copy(s,p+1,ls-p-1),t1,code);

    up:=up+t1*down;

    t:=up;up:=down;down:=t;

    ls:=p;

    until ls=1;

    t:=up;up:=down;down:=t;

    writeln(up,'/',down);

    end

    else begin

    p:=pos('/',s);

    val(copy(s,1,p-1),t1,code);

    delete(s,1,p);

    val(s,t2,code);

    write('[');

    write(t1 div t2);

    if t1 mod t2=0 then begin writeln(']');continue;end

    else write(';');

    repeat

    t1:=t1 mod t2;

    t:=t1;t1:=t2;t2:=t;

    write(t1 div t2);

    if t1 mod t20 then write(',');

    until t1 mod t2=0;

    writeln(']');

    end;

    end;

    end.

  • -1
    @ 2009-11-08 09:11:46

    额...

    沙茶了...

  • -1
    @ 2009-11-08 08:55:32

    果然 不能输出 x/1 这种东西 不讲道理呀....

  • -1
    @ 2009-11-08 08:44:11

    program no1;

    var st:string;

    function zzxc(m,n:int64):int64;

    var a,b,r:int64;

    begin

    a:=m;b:=n;

    repeat

    r:=a mod b;

    a:=b; b:=r;

    until r=0;

    zzxc:=a;

    end;

    procedure doit(st:string);

    var s,s1,s2,ss1,ss2:int64;

    j,k,i:longint;

    a:array[0..200] of longint;

    bz:boolean;

    begin

    if st[1]='[' then

    begin

    fillchar(a,sizeof(a),0);

    j:=2;k:=0;

    while j='0') and (st[j]

  • -1
    @ 2009-11-08 08:37:24

    我比赛的时候考虑到分子很大,分母很小的情况。于是计算不大于某个数的整数时二分了。结果对于某些分母较大的整数,在二分过程中会超过longint。改成循环后80分,结果发现漏了一种情况,即[x]是不用数出分母1的(考虑了约分竟然没考虑这个)。

  • -1
    @ 2009-11-08 08:29:41

    program test1;

    const re:set of char=(['0','1','2','3','4','5','6','7','8','9']);

    var i,j,k,m:longint;

    s1,s2:ansistring;

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

    begin

    if b=0 then gcd:=a else gcd:=gcd(b,a mod b);

    end;

    procedure work1;

    var i,j,k,m,n,fz,fm,t:longint;

    x:array[1..10000] of longint;

    boo:boolean;

    Begin

    i:=2;j:=1; boo:=false;

    if s1[i]='-' then begin inc(i);boo:=true;end;

    while ifm then

    begin

    for i:= n-1 downto 1 do

    begin

    t:=fm;fm:=fz;fz:=t;

    fz:=fz+x[i]*fm;

    end;

    m:=gcd(fm,fz);

    fm:=fm div m;

    fz:=fz div m;

    end;

    if fm

  • -1
    @ 2009-11-08 08:27:54

    完美版

    function gcd(p,q:longint):longint;

    var

    temp:longint;

    begin

    if q=0 then gcd:=p

    else gcd:=(q,p mod q);

    end;

    begin

    while not eof do

    begin

    readln(s);

    len:=length(s);

    fillchar(a,sizeof(a),0);

    if s[1]='[' then

    begin

    num:=1;ex:=10;

    for i:=2 to len-1 do

    begin

    if (s[i]=';') or (s[i]=',') then inc(num)

    else

    begin

    a[num]:=a[num]*ex+ord(s[i]-'0');

    end;

    end;

    if num=1 then writeln(a[1])

    else

    begin

    up:=1;down:=a[num];

    for i:=num-1 downto 1 do

    begin

    up:=up+a[i]*down;

    r:=up;up:=down;down:=r;

    g:=gcd(up,down);

    up:=up div g;down:=down div g;

    end;

    if up1 then

    writeln(down,'/',up)

    else writeln(down);

    end;

    end

    else

    begin

    l:=0;

    while s[l]'/' do inc(l);

    ex:=10;pp:=0;dd:=0;

    for i:=1 to l-1 do

    pp:=pp*ex+ord(s[i]-'0');

    ex:=10;

    for i:=l+1 to len do

    dd:=dd*ex+ord(s[i]-'0');

    write('[');

    g:=gcd(pp,dd);ex:=1;

    pp:=pp div g;dd:=dd div g;

    while dd1 do

    begin

    if ex=1 then

    begin

    write(pp div dd,';');

    inc(ex);

    end

    else write(pp div dd,',');

    pp:=pp-pp div dd*dd;

    temp:=pp;pp:=dd;dd:=temp;

    end;

    writeln(pp,']');

    end;

    end;

    end.

  • -1
    @ 2009-11-08 07:48:32

    2/1到底是什么啊啊啊啊

  • -1
    @ 2009-11-08 07:46:47

    编译通过...

    测试数据01:答案正确... 0ms

    测试数据02:答案正确... 0ms

    测试数据03:答案正确... 0ms

    测试数据04:答案错误...程序输出比正确答案长

    测试数据05:答案正确... 0ms

    ---|---|---|---|---|---|---|---|-

    Unaccepted 有效得分:80 有效耗时:0ms

    why??????????????????

  • -1
    @ 2009-11-08 08:57:43

    不知道哪里错了。。和一个AC的程序对拍一直是正确的。。。悲剧。

    #include

    #include

    char buffer[2001],*buf;

    inline long upfloor(double x)

    {

    if(x-(long)x-0.00000000000000001)return (long)x+1;

    return (long)x;

    }

    inline long getw(long x)

    {

    if(!x)return 1;

    if(x>0)

    return upfloor(log10(x));

    return upfloor(log10(-x))+1;

    }

    void ctof(long &a,long &b)

    {

    long t,t1,t2;

    if(sscanf(buf,"%d",&t)>0)

    {

    buf+=getw(t)+1;

    ctof(t1,t2);

    b=t1;

    a=t*t1+t2;

    }

    else

    {

    a=1;b=0;

    }

    }

    void ftoc(long a,long b)

    {

    if(!a)return ;

    printf("%d,",b/a);

    b%=a;

    ftoc(b,a);

    }

    void inline swap(long &a,long &b)

    {

    long t=a;

    a=b;

    b=t;

    }

    long Euclid(long a,long b)

    {

    if(ab?a-b:b-a,a>b?b:a);

    else if(b&1)return Euclid(a>>1,b);

    else if(a&1)return Euclid(a,b>>1);

    else return Euclid(a>>1,b>>1);

    }

    void yf(long& a,long& b)

    {

    long register tmp=Euclid(a,b);

    a/=tmp;b/=tmp;

    }

    int main()

    {

    freopen("fraction.in","r",stdin);

    freopen("fraction.out","w",stdout);

    while(scanf("%s",buffer)!=EOF)

    if(buffer[0]=='[')

    {

    long a,b,t;

    sscanf(buffer,"[%d",&t);

    buf=buffer+getw(t)+2;

    ctof(a,b);

    yf(a,b);

    if(a-1)

    printf("%d/%d\n",b+t*a,a);

    else

    printf("%d\n",b+t);

    }

    else

    {

    long a,b;

    sscanf(buffer,"%d/%d\n",&a,&b);

    //printf("%d/%d=",a,b);

    if(b=0)

    {

    yf(a,b);

    printf("[%d;",a/b);

    a%=b;

    }

    else

    {

    long tmp=-a;

    yf(tmp,b);

    if(tmp%b)

    {

    printf("[-%d;",tmp/b+1);

    a=b-tmp%b;

    }

    else

    {

    printf("[-%d;",tmp/b);

    a=0;

    }

    }

    ftoc(a,b);

    fseek(stdout,-1,SEEK_CUR);

    printf("]\n");

    }

    return 0;

    }

  • -1
    @ 2009-11-08 15:51:03

    编译通过...

    ├ 测试数据 01:答案正确... 0ms

    ├ 测试数据 02:答案正确... 0ms

    ├ 测试数据 03:答案正确... 0ms

    ├ 测试数据 04:答案正确... 0ms

    ├ 测试数据 05:答案正确... 0ms

    ---|---|---|---|---|---|---|---|-

    Accepted 有效得分:100 有效耗时:0ms

    后来!

    编译通过...

    ├ 测试数据 01:答案正确... 0ms

    ├ 测试数据 02:答案正确... 0ms

    ├ 测试数据 03:答案正确... 0ms

    ├ 测试数据 04:答案错误...程序输出比正确答案长

    ├ 测试数据 05:答案正确... 0ms

    ---|---|---|---|---|---|---|---|-

    Unaccepted 有效得分:80 有效耗时:0ms

    为啥?

  • -1
    @ 2009-11-08 01:00:21

    调了半天,原来是分母为1的时候不输出

    交了n多次

  • -1
    @ 2009-11-08 00:31:29

    占楼~~orz

  • -1
    @ 2009-11-08 00:08:09

    *|可能不是满分程序

    *
    |也可能是

    #include

    #include

    char s[10000];

    int main()

    {

    for(;scanf("%s",s)!=-1;)

    {

    if(s[0]=='[')//连分数---|>分数

    {

    int j=0,locate=1,number;

    long turn[10000]={};

    for(number=0;locate='0'&&s[locate]=0;j--)

    {

    p1=p1+turn[j]*p2;

    int temp=p1;

    p1=p2;

    p2=temp;

    }

    int temp=p1;

    p1=p2;

    p2=temp;

    for(j=p2;j>0;j--)

    if(p1%j==0&&p2%j==0)

    {

    p1/=j;

    p2/=j;

    break;

    }

    if(p2>1)printf("%d/%d\n",p1,p2);

    else printf("%d\n",p1);

    }

    else

    {

    int j=0,locate=0,number;

    long turn[2]={};

    for(number=0;locate='0'&&s[locate]

  • -1
    @ 2009-11-07 23:56:00

    mao2

  • -1
    @ 2009-11-07 21:48:39

    我算什么?

信息

ID
1696
难度
7
分类
其他 | 数学模拟 点击显示
标签
递交数
5142
已通过
1133
通过率
22%
被复制
3
上传者