主题:NOIP2002复赛第一题解法
输入一个K(K<=15),S=1+1/2+1/3+……+1/N,当S>K成立时,输出最小的N值。
这有用FOR循环、WHILE循环、REPEAT-UNTIL循环。
program noipc1;
var k:byte;{BYTE型的范围-255~255} n:longint;{长整范围10^-17~10^-18} s:real;
begin
readln(k);{输入K值}
n:=1;
for n:=1 to 10000000 do begin
s:=s+1/n;
if s>k then begin
writeln(n); break;{跳出循环}
end;
end;
end.
program noipc1;
var
begin
readln(k);
n:=1;
while s>k do begin
s:=s+1/n;
if s<=k then
n:=n+1;
end;
writeln(n);
end.
program noipc1;
var
begin
readln(k);
n:=1;
repeat
s:=s+1/n;
if s<=k then
n:=n+1;
until s<=k;
writeln(n);
end.
这有用FOR循环、WHILE循环、REPEAT-UNTIL循环。
program noipc1;
var k:byte;{BYTE型的范围-255~255} n:longint;{长整范围10^-17~10^-18} s:real;
begin
readln(k);{输入K值}
n:=1;
for n:=1 to 10000000 do begin
s:=s+1/n;
if s>k then begin
writeln(n); break;{跳出循环}
end;
end;
end.
program noipc1;
var
begin
readln(k);
n:=1;
while s>k do begin
s:=s+1/n;
if s<=k then
n:=n+1;
end;
writeln(n);
end.
program noipc1;
var
begin
readln(k);
n:=1;
repeat
s:=s+1/n;
if s<=k then
n:=n+1;
until s<=k;
writeln(n);
end.