回 帖 发 新 帖 刷新版面

主题:[color=FF0000]紧急!!紧急!!![/color]

输入a,b,输出a/b的小数部分,但是如果是循环小数,则用{x}标出.
[size=6][color=FF0000]注:x表示循环节.[/color][/size]

回复列表 (共2个回复)

沙发

编出程序的有分加++++++++++++++++++++++++++

板凳

//初学 pascal,可能有误
program two;
{$APPTYPE CONSOLE}

procedure _div(a :Integer; b :Integer; var c :array of Char; var l :Integer);
var
    bPoint :Boolean;
    i, j, k :Integer;
begin
    bPoint := False;
    
    i := 0;
    while (i < l) and (a <> 0) do
    begin
        if a < b then //被除数小于除数, 被除数后面加零(即被除数乘以 10)
        begin
            if not bPoint then //小数点处理
            begin
                if i = 0 then
                begin
                    c[i] := '0';
                    Inc(i);
                end;
                c[i] := '.';
                Inc(i);

                bPoint := True;
            end;

            a := a * 10;
        end;
        
        k := a div b;
        j := 1;
        while j <= k do
            j := j * 10;
        j := j div 10;
        while j >= 1 do
        begin
            c[i] := Char($30 + k div j);
            Inc(i);

            k := k mod j;
            j := j div 10;
        end;

        a := a mod b;
    end;
    l := i;
    c[l] := #0;
end;


var
    buff :array[1..100] of Char;
    a, b, i, j1, j2, j3, l:Integer;
    bb :Boolean;

begin
    Writeln('请输入 a, b:');
    Read(a, b);
    
    if b = 0 then
    begin
        Writeln('除数不可为零!');
        Exit;
    end;
    
    l := 100;
    _div(a, b, buff, l);
    
    i := 1;
    bb := True;
    while (i < l) and bb do
    begin
        if buff[i] = '.' then
        begin
            j1 := i;
            while (j1 <= l - 1) and bb do
            begin
                j2 := j1 + 1;
                while (j2 <= l) and bb do
                begin
                    if buff[j1] = buff[j2] then
                    begin
                        j3 := 1;
                        while (j3 < j2 - j1) and (buff[j1 + j3] = buff[j2 + j3]) do
                            Inc(j3);
                        if j3 = j2 - j1 then bb := False;
                    end;
                    Inc(j2);
                end;
                Inc(j1);
            end;
        end;
        Inc(i);
    end;

    if bb then
        Writeln(buff)
    else begin
        i := 1;
        while i < j2 - 1 do
        begin
            if i = j1 - 1 then Write('{');
            Write(buff[i]);
            Inc(i);
        end;
        Write('}');
    end;
end.

我来回复

您尚未登录,请登录后再回复。点此登录或注册