Pascal

Modified: Wednesday, 22-12-2021 07:00 AM

Cho phân số có:

tử < mẫu  

tử <1.000 và mẫu <1.000.000

Tách phân số thành tổng của 5 phân số có tử bằng 1

VD:

Nhap Tu so: 79
Nhap Mau so: 654
--------
Phan so rut gon = 79/654
Phan so bien doi = 237/1962
--------
= 1/9 + 1/218 + 1/327 + 1/654 + 1/1962
 


var tu, mau, ts, ms, UC:longint;
    aMau:array[1..2000] of longint;
        i,n:longint;
    bo:boolean;

// Tim UCLN
function UCLN(x,y:longint):longint;
begin
    if x mod y = 0 then UCLN:=y 
        else UCLN:=UCLN(y,x mod y);
end;    
// Duyet Tu so
procedure DuyetTuSo(x:longint);
var a,b,c,d,e:longint;
begin
    for a:=1 to n-4 do 
        for b:= a+1 to n-3 do 
        for c:= b+1 to n-2 do
        for d:= c+1 to n-1 do 
        for e:= d+1 to n do 
        if (aMau[a]+aMau[b]+aMau[c]+aMau[d]+aMau[e])=ts then 
    begin
        bo := true;
        writeln('Phan so bien doi = ',ts,'/',ms);
        writeln('--------');
        writeln('= 1/',ms div aMau[e],' + 1/',ms div aMau[d],' + 1/',ms div aMau[c],' + 1/',ms div aMau[b],' + 1/',ms div aMau[a]);
        exit;
    end;
    
end;
//phan tich mau ra thua so nguyen to
procedure PhanTichMau(x:longint);
begin
    n := 0;
    for i:=1 to (x div 2) do 
        if x mod i = 0 then  
    begin
        inc(n);
        aMau[n]:=i;
    end;
    if n>=5 then
        DuyetTuSo(ts);    
end;
// Main
begin
    write('Nhap Tu so: ');
    readln(tu);
    write('Nhap Mau so: ');
    readln(mau);
    UC  := UCLN(tu,mau);
    tu  := tu div UC;
    mau := mau div UC;
    writeln('--------');
    writeln('Phan so rut gon = ',tu,'/',mau);
    ts := 0;
    ms := 0;
    bo := false;
    while bo=false do 
    begin
        ts := ts+tu;
        ms := ms+mau;
        PhanTichMau(ms);
    end;
    readln;
end.