Thứ Sáu, 13 tháng 5, 2022

BIEUTHUC1235 Pascal

type ps = record
    t,m:longint;
end;

var     n,i,d:longint;
        a,p:array[1..1000000] of ps;
        dau:array[1..100000] of char;

function gcd(a,b:int64):int64;
var t:longint;
begin
    while b>0 do
    begin
           t:=a mod b;
           a:=b;
           b:=t;
    end;
    exit(a);
end;

function toigian(a:ps):ps;
var ucln:longint;
begin
    ucln:=gcd(a.t,a.m);
    a.t:=a.t div ucln;
    a.m:=a.m div ucln;
    if a.m<0 then
    begin
        a.t:=-a.t;
        a.m:=-a.m;
    end;
   exit(a);
end;

function cong(a,b:ps):ps;
var ucln:longint;
        c:ps;
begin
   ucln:=gcd(a.m,b.m);
   c.m:=(a.m*b.m) div ucln;
   c.t:=a.t*(c.m div a.m)+b.t*(c.m div b.m);
   exit(c);
end;

function nhan(a,b:ps):ps;
var c:ps;
begin
   c.m:=a.m*b.m;
   c.t:=a.t*b.t;
   exit(c);
end;

procedure tien_xu_li;

begin
    a[1]:=p[1];d:=1;
    for i:=1 to n-1 do
    begin
        if dau[i]='+' then
        begin
            inc(d);
            a[d]:=p[i+1];
        end;
        if dau[i]='-' then
        begin
            p[i+1].t:=-p[i+1].t;
            inc(d);
            a[d]:=p[i+1];
        end;
        if dau[i]='*' then
            a[d]:=nhan(a[d],p[i+1]);
    end;
end;

procedure tinh;
var res:ps;
begin
    res:=a[1];
    for i:=2 to d do
        res:=cong(res,a[i]);
    res:=toigian(res);
    write(res.t, ' ',res.m);
end;

procedure nhap;
begin
    readln(n);
    for i:=1 to n do
        readln(p[i].t,p[i].m);
    for i:=1 to n-1 do
        readln(dau[i]);
end;
begin
    assign(input,'bieuthuc1235.inp');reset(input);
    assign(output,'bieuthuc1235.out');rewrite(output);
    nhap;
    tien_xu_li;
    tinh;
end.

Không có nhận xét nào:

Đăng nhận xét

Lưu ý: Chỉ thành viên của blog này mới được đăng nhận xét.