题解

70 条题解

  • -1
    @ 2016-11-20 21:43:28
    var
    a:ansistring;
    b:array[1..100000]of longint;
    i,n,j,k,num1,num2,x,t:longint;
    function pd(n:longint):boolean;
    begin
    if n in [48..57] then pd:=false else pd:=true;
    end;
    begin
    while not eof do begin
    readln(a);
    n:=length(a);
    if pd(ord(a[1])) then begin
    i:=2;x:=0;j:=1;num1:=1;
    while i<n do begin repeat x:=x*10+(ord(a[i])-ord('0')); inc(i); until (pd(ord(a[i]))) or (i>n);
    inc(i);
    b[j]:=x;
    x:=0;
    inc(j);
    end;
    dec(j);
    if j=1 then writeln(b[1]) else begin
    num2:=b[j];
    for k:=j downto 1 do
    begin
    num1:=b[k-1]num2+num1;
    t:=num1; num1:=num2; num2:=t;
    end;
    writeln(num1,'/',num2);
    end;
    end else begin
    i:=1;x:=0;j:=0;num1:=0;num2:=0;
    while i<=n do
    begin
    repeat
    x:=x10+(ord(a[i])-ord('0'));
    inc(i);
    until (pd(ord(a[i]))) or (i>n);
    inc(i);
    if j=0 then begin num1:=num1+x; inc(j); x:=0;
    end else num2:=num2+x;
    end;
    if num2=0 then writeln('[',num1,']')else
    if num1 mod num2=0 then writeln('[',num1 div num2,']')
    else begin
    write('[');
    for k:=1 to 1000 do begin
    b[k]:=num1 div num2;
    num1:=num1 mod num2;
    t:=num1; num1:=num2; num2:=t;
    if k=1 then write(b[k],';')
    else if num2<1 then begin writeln(b[k],']');break;end else write(b[k],',');
    end;
    end;
    end;
    end;
    end.
    
    • @ 2017-08-13 18:46:50

      num1:=b[k-1]num2+num1; ???编译错误

    • @ 2017-08-13 18:47:45

      还有
      x:=x10+(ord(a[i])-ord('0'));
      x10哪来的?

  • -1
    @ 2016-05-26 21:17:40
    program lx;
    var str:string;
    flag,s:longint;
    a:array [1..1000] of longint;
    procedure init;
    var sum,i,weight:longint;
    ok:boolean;
    begin
    readln(str);
    if str[length(str)]=']' then flag:=0 else flag:=1;
    weight:=1;
    s:=0;
    i:=length(str);
    while i>0 do
    begin
    sum:=0;
    ok:=false;
    weight:=1;
    while str[i] in ['1'..'9'] do
    begin
    ok:=true;
    sum:=sum+(ord(str[i])-ord('0'))*weight;
    weight:=weight*10;
    dec(i);
    end;
    if ok then
    begin
    inc(s);
    a:=sum;
    end;
    dec(i);
    end;
    if flag=0 then
    if str[2]='-' then a:=a*(-1);
    if flag=1 then
    if str[1]='-' then a:=a*(-1);
    end;
    
    function gcd(a,b:longint):longint;
    begin
    if b=0 then gcd:=a
    else gcd:=gcd(b,a mod b);
    end;
    
    
    procedure work0;
    var fm,fz,i,temp:longint;
    begin
    fm:=a[1];
    fz:=1;
    for i:=2 to s do
    begin
    fz:=fz+a[i]*fm;
    temp:=fz;
    fz:=fm;
    fm:=temp;
    end;
    if fz div gcd(fz,fm)=1 then writeln(fm div gcd(fz,fm))
    else if fz div gcd(fz,fm)<0 then
    writeln((fm div gcd(fz,fm))*(-1),'/',(fz div gcd(fz,fm))*(-1))
    else writeln(fm div gcd(fz,fm),'/',fz div gcd(fz,fm));
    end;
    
    
    procedure work1;
    var fz,fm,k,temp,h:longint;
    ok:boolean;
    begin
    write('[');
    fz:=a[2];
    fm:=a[1];
    if fz>=0 then
    begin
    k:=gcd(fz,fm);
    fz:=fz div k;
    fm:=fm div k;
    write(fz div fm,';');
    end
    else
    begin
    h:=0;
    while fz<0 do begin inc(fz,fm);dec(h);end;
    write(h,';');
    end;
    fz:=fz mod fm;
    ok:=false;
    while fz<>1 do
    begin
    temp:=fz;
    fz:=fm;
    fm:=temp;
    if not ok then begin write(fz div fm);ok:=true;end
    else write(',',fz div fm);
    fz:=fz mod fm;
    end;
    writeln(',',fm,']');
    end;
    
    
    
    begin
    while not eof do
    begin
    init;
    if flag=0 then work0 else work1;
    fillchar(a,sizeof(a),0);
    str:='';
    end;
    end.
    
  • -1
    @ 2016-03-13 13:07:47
    uses math;
    function gcd(a,b:longint):longint;
    begin
      if b=0 then gcd:=a
      else gcd:=gcd(b,a mod b);
    end;
    
    
    
    //繁分数->分数
    procedure transto(var s:string);
    var 
      i,b,x,n,d,code:longint;temp:string;
      a:array[0..200] of longint;
      len:longint;
    begin
      len:=0;
      n:=0;d:=0;
      for i:=length(s)-1 downto 1 do begin
        if (s[i]=',') or (s[i]=';') or (s[i]='[') then begin
          val(copy(s,i+1,len),x,code);
          if d=0 then begin n:=1;d:=x
          end else begin
            n:=x*d+n;
            code:=n;n:=d;d:=code;
          end;
          len:=0
        end else inc(len);
      end;
      code:=n;n:=d;d:=code;
      code:=gcd(n,d);n:=n div code;d:=d div code;
      if d=1 then str(n,s)
      else begin
        str(n,s);
        str(d,temp);
        s:=s+'/'+temp;
      end;
    end;
    
    //分数->繁分数
    procedure transfrom(var s:string);
    var n,i,d,code,g:longint;res,temp:string;
    begin
      i:=-1;
      for i:=1 to length(s) do
        if s[i]='/' then begin
          val(copy(s,1,i-1),n,code);
          val(copy(s,i+1,length(s)-2),d,code);
        end;
      if i=-1 then begin write(s);exit end;
      g:=gcd(n,d);n:=n div g;d:=d div g;
      
      if n mod d=0 then begin write(n div d);exit end;
      
      str(n div d,temp);
      res:='['+temp+';';
      code:=d;d:=n mod d;n:=code;
      while d<>1 do begin
        str(n div d,temp);
        res:=res+temp+',';
        code:=d;d:=n mod d;n:=code;
        if d=0 then begin write(n div d);break end;
        end;
      if n<>1 then begin
        str(n,temp);
        res:=res+temp+',';
      end;
      res[length(res)]:=']';
      s:=res;
    end;
    
    var s:string;
      a:array[0..100] of string;
      i,count:longint;
    begin //main
      while not eof do begin
        readln(s);
        if s[1]='[' then transto(s) else transfrom(s);
        inc(count);
        a[count]:=s;
      end;
      for i:=1 to count do writeln(a[i]);
    end.
    
  • -1
    @ 2009-11-10 10:39:27

    楼下的说得很对

    注意了:[4]---|4

    2/1---|[2]

    就ac了

  • -1
    @ 2009-11-09 21:54:50

    这个C++真的很诡异:

    不同的读入方式分数居然不一样………………

    cin>>s;

    while(!cin.eof())

    {top=0;

    memset(a,0,sizeof(a));

    if(s[0]=='[') solve1();

    else solve2();

    cin>>s;

    }

    20分,除第四点外其余全错

    while(!cin.eof())

    {cin>>s; top=0;

    memset(a,0,sizeof(a));

    if(s[0]=='[') solve1();

    else solve2();

    }

    80分,第四个点错

    do

    {cin>>s; top=0;

    memset(a,0,sizeof(a));

    if(s[0]=='[') solve1();

    else solve2();

    }while(!cin.eof())

    80分,第三个点格式错误 (- -!)

    最后,忍无可忍……

    while(getline(cin,s))

    {top=0;

    memset(a,0,sizeof(a));

    if(s[0]=='[') solve1();

    else solve2();

    }

    终于AC

    用C++的同志看来得注意一下

  • -1
    @ 2009-11-08 23:28:10

    我终于知道了...

    第4组有这种情况

    [4]->4

    2/1->[2]

    而不是[4;]->4/1

    !!!!!!!!!!!!!!!!

    我的AC率..........

  • -1
    @ 2009-11-08 21:29:33

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

    BS这种无聊的题目……BS出题人这等恶心的数据……

  • -1
    @ 2009-11-08 20:49:16

    编译通过...

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

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

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

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

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

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

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

    交了无数次,提醒下大家在分数转连分数过程中有可能会有例如2/4的情况,所以不能以分子=1为退出条件

  • -1
    @ 2009-11-08 20:20:13

    就是哈,第4个点是什么回事啊 ??//

    老是207???

    哎,那个大牛提示一下????????/

  • -1
    @ 2009-11-08 18:58:33

    对此题极度。。无语。。

    X/1 要输出 X 。。。

  • -1
    @ 2009-11-08 17:24:40

    谁能解释下题目?

  • -1
    @ 2009-11-08 14:17:11

    几个阴人的地方

    1. 3/1---|> 3
    2. [3;]---|>[3]

    其中个人觉得第1点不合理 类似3这种,根本不是分数,应该写成3/1才对

  • -1
    @ 2009-11-08 13:46:54

    第4个点到底是什么啊?

  • -1
    @ 2009-11-08 12:38:41

    第4个点到底什么意思啊!!!

  • -1
    @ 2009-11-08 11:22:52

    program lx;

    var str:string;

    flag,s:longint;

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

    procedure init;

    var sum,i,weight:longint;

    ok:boolean;

    begin

    readln(str);

    if str[length(str)]=']' then flag:=0 else flag:=1;

    weight:=1;

    s:=0;

    i:=length(str);

    while i>0 do

    begin

    sum:=0;

    ok:=false;

    weight:=1;

    while str[i] in ['1'..'9'] do

    begin

    ok:=true;

    sum:=sum+(ord(str[i])-ord('0'))*weight;

    weight:=weight*10;

    dec(i);

    end;

    if ok then

    begin

    inc(s);

    a:=sum;

    end;

    dec(i);

    end;

    if flag=0 then

    if str[2]='-' then a:=a*(-1);

    if flag=1 then

    if str[1]='-' then a:=a*(-1);

    end;

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

    begin

    if b=0 then gcd:=a

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

    end;

    procedure work0;

    var fm,fz,i,temp:longint;

    begin

    fm:=a[1];

    fz:=1;

    for i:=2 to s do

    begin

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

    temp:=fz;

    fz:=fm;

    fm:=temp;

    end;

    if fz div gcd(fz,fm)=1 then writeln(fm div gcd(fz,fm))

    else if fz div gcd(fz,fm)=0 then

    begin

    k:=gcd(fz,fm);

    fz:=fz div k;

    fm:=fm div k;

    write(fz div fm,';');

    end

    else

    begin

    h:=0;

    while fz

  • -1
    @ 2009-11-08 10:55:24

    悲剧啊,和ac程序输出一样,和测试数据也一样,却爆了一个点。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

  • -1
    @ 2009-11-08 16:13:21

    我的题解。。

    幸好把这些WS的请况都想了个便

    http://hi.baidu.com/木子日匀/blog/item/2e664e0f92a2cfed36d122d0.html

  • -1
    @ 2009-11-08 10:32:12

    VAR

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

    s:string;

    w,p,l,m,y,i,n,key,x:longint;

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

    begin

    if b=0 then gcd:=a

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

    end;

    procedure change(var a,b:longint);

    var l:longint;

    begin

    l:=a;

    a:=b;

    b:=l;

    end;

    procedure init;

    begin

    readln(s);

    if s[1]='['then

    begin

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

    if y=0 then begin val(copy(s,2,length(s)-2),x);writeln(x);exit;end;

    w:=y;

    val(copy(s,2,w-2),a[1]);

    delete(s,1,w);

    p:=1;

    repeat

    inc(p);

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

    if w0 then

    begin

    val(copy(s,1,w-1),a[p]);

    delete(s,1,w);

    end;

    until w=0;

    l:=length(s);

    val(copy(s,1,l-1),a[p]);

    end

    else

    begin

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

    w:=y;

    val(copy(s,1,w-1),a[1]);

    val(copy(s,w+1,length(s)-w),a[2]);

    key:=gcd(a[1],a[2]);

    a[1]:=a[1]div key; a[2]:=a[2]div key;

    end;

    end;

    procedure main;

    begin

    if y=0 then exit;

    if p0then

    begin

    n:=a[p];

    dec(p);

    m:=a[p]*n+1;

    change(m,n);

    if p>1 then

    begin

    repeat

    dec(p);

    m:=n*a[p]+m;

    change(m,n);

    key:=gcd(m,n);

    m:=m div key;

    n:=n div key;

    until p=1;

    end;

    writeln(n, '/',m);

    end

    else

    begin

    if a[2]=1then begin writeln('[',a[1],']');exit;end;

    write('[');

    write(a[1]div a[2],';');

    a[1]:=a[1]mod a[2];

    change(a[1],a[2]);

    while a[2]1 do

    begin

    write(a[1]div a[2],',');

    a[1]:=a[1]mod a[2];

    change(a[1],a[2]);

    end;

    write(a[1],']'); writeln;

    end

    end;

    begin

    while not eof do

    begin

    p:=0;

    init;

    main;

    end;

    end.

  • -1
    @ 2009-11-08 10:19:07

    考的时候想到了X/1的情况,也写了,可就是没想到【X】的情况

    比如【2,1】我答案是3,【3】就成了10/3了,悲剧

  • -1
    @ 2009-11-08 09:59:15

    呃呃呃

    题目没说要把X/1输成X吧

    难道是让我们去猜题目要求??

信息

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