' QBASIC CONST n = 10 DIM a(1 TO n) AS DOUBLE, b(1 TO n) AS DOUBLE, x(1 TO n) AS DOUBLE RANDOMIZE TIMER CLS FOR i = 1 TO n a(i) = 50 * RND - 25 b(i) = 50 * RND - 25 IF a(i) <> 0 THEN x(i) = b(i) / a(i) ELSE x(i) = 0 END IF PRINT USING "###."; a(i); PRINT " * "; PRINT USING "###."; x(i); PRINT " = "; PRINT USING "###."; b(i) NEXT i
// PascalABC.NET 3.1, сборка 1174 от 22.02.2016
begin
Writeln(Range(1,100).Select(i->sin(i)*cos(i)).Where(x->x<>0).Average)
end.
Тестовое решение:
-0.00136006072493969
2. А вот так учат писать это же школьные учителя:
// PascalABC.NET 3.1, сборка 1174 от 22.02.2016
var
m:array[1..100] of real;
i,k:integer;
s:real;
begin
s:=0;
k:=0;
for i:=1 to 100 do begin
m[i]:=sin(i)*cos(i);
if m[i]<>0 then begin
s:=s+m[i];
k:=k+1
end
end;
Writeln(s/k)
end.
Тестовое решение:
-0.00136006072493969
CONST n = 10
DIM a(1 TO n) AS DOUBLE, b(1 TO n) AS DOUBLE, x(1 TO n) AS DOUBLE
RANDOMIZE TIMER
CLS
FOR i = 1 TO n
a(i) = 50 * RND - 25
b(i) = 50 * RND - 25
IF a(i) <> 0 THEN
x(i) = b(i) / a(i)
ELSE
x(i) = 0
END IF
PRINT USING "###."; a(i);
PRINT " * ";
PRINT USING "###."; x(i);
PRINT " = ";
PRINT USING "###."; b(i)
NEXT i
Тестовое решение:
-14.65854 * 0.53867 = -7.89606
-14.19729 * 1.08311 = -15.37722
-17.21156 * -0.07488 = 1.28888
16.17024 * -1.09750 = -17.74690
-13.80126 * -1.06180 = 14.65417
17.78583 * 0.83055 = 14.77207
-10.95534 * -1.58899 = 17.40791
-11.84992 * 1.66222 = -19.69714
-24.91831 * -0.95948 = 23.90864
-12.68757 * 0.84160 = -10.67785