Решение задачи 9.1.
Алгоритм:
p:=0; q:=1;
metka:
if p/q < r then p:=p+1;
if p/q = r then stop;
if p/q > r then q:=q+1;
if q > qmax then stop;
goto metka;
PROGRAM ratap;
VAR p,q,qmax:integer;
d, r, min: real;
BEGIN
write('r,qmax='); readln(r,qmax);
p:=0; q:=1; min:=r; REPEAT
IF p/q<r THEN p:=p+1 ELSE q:=q+1;
d:=abs(r-p/q);
IF d<min THEN BEGIN min:=d; writeln(p:7,'/',q) END
UNTIL (q>=qmax) OR (d=0);
readln; END.
Решение задачи взята из книги "Еxploring mathematics with your computer" by Arthur Engel (The Mathematical Association of America, New Mathematical Library, 1993)
|