Program primer; procedure random(c,x,y:integer; var r:integer); begin case c of 1 : r:=Random(ABS(Y)+ABS(x)+1)+x; 2 : r:=Random(ABS(Y)+ABS(x)+1)+y; 3 : r:=Random(Y-x+1)+x; 4 : r:=Random(x-y+1)+y; 5 : r:=Random(x+y); 6 : r:=Random(ABS(x+y))-ABS(x+y); 7 : r:=x; 8 : r:=Random(ABS(ABS(x)-ABS(y)))-ABS(x); 9 : r:=Random(ABS(ABS(x)-ABS(y)))-ABS(y); end; end; var a: array of array of integer; i,j,x,y,m,p,pro,c:integer; begin Randomize; pro:=1; writeln('Кол-во строк и столбцов массива? //X,Y\\ '); read(i,j); //Размерность массива writeln('Диапазон чисел //от X,до Y\\ '); read(x,y); //и диапазон генерации вводятся с клавиатуры. if (x=y) then c:=7 else if(x<0) and (y<0) then if (x<y) then c:=8 else c:=9 else if (x=0) or (y=0) then if(x<0) or (y<0) then c:=6 else c:=5 else if(x<0) or (y<0) then if (x<y) then c:=1 else c:=2 else if (x<y) then c:=3 else c:=4; SetLength(a,i); for m:=0 to i-1 do SetLength(a[m],j); for m:=0 to i-1 do begin writeln(); for p:=0 to j-1 do begin random(c,x,y,a[m,p]); {Заполнить матрицу случайными числами} if (m<>p) then pro:=pro*a[m,p]; {Вычислить произведение элементов массива, лежащих вне главной диагонали.} write (a[m,p]:3,' ':3); end; end;
int main() { int i,n; cout<<"Введите число элементов в массиве: "; cin>>n; int a[n-1], j=0, amin=32767; for (i=0; i<n; i++){ cout<<"Введите "<<i+1<<"-й элемент: "; cin>>a[i]; if (amin>a[i] && a[i]>0) { amin=a[i]; j=i; } } i=n/2; while ((i<n) and a[i]>=0) i++; if (i>=n) if (a[n-1]<0) i=n-1; else cout<<"Нет подходящего элемента"; if (i<=n-1) { int s=0, k=0, t; if (i>j) { t=i; i=j; j=t; } for (i=i; i<=j; i++) { s+=a[i]; k++; } cout<<"Cумма "<<s<<", количество "<<k<<"\n"; } system("PAUSE"); return 0; }
Тестовое решение: Введите число элементов в массиве: 10 Введите 1-й элемент: 8 Введите 2-й элемент: 5 Введите 3-й элемент: -3 Введите 4-й элемент: 0 Введите 5-й элемент: 4 Введите 6-й элемент: 7 Введите 7-й элемент: -5 Введите 8-й элемент: 11 Введите 9-й элемент: 2 Введите 10-й элемент: -6 Cумма 8, количество 3 Для продолжения нажмите любую клавишу . . .
procedure random(c,x,y:integer; var r:integer);
begin
case c of
1 : r:=Random(ABS(Y)+ABS(x)+1)+x;
2 : r:=Random(ABS(Y)+ABS(x)+1)+y;
3 : r:=Random(Y-x+1)+x;
4 : r:=Random(x-y+1)+y;
5 : r:=Random(x+y);
6 : r:=Random(ABS(x+y))-ABS(x+y);
7 : r:=x;
8 : r:=Random(ABS(ABS(x)-ABS(y)))-ABS(x);
9 : r:=Random(ABS(ABS(x)-ABS(y)))-ABS(y);
end;
end;
var
a: array of array of integer;
i,j,x,y,m,p,pro,c:integer; begin
Randomize;
pro:=1;
writeln('Кол-во строк и столбцов массива? //X,Y\\ ');
read(i,j); //Размерность массива
writeln('Диапазон чисел //от X,до Y\\ ');
read(x,y); //и диапазон генерации вводятся с клавиатуры.
if (x=y) then
c:=7
else
if(x<0) and (y<0) then
if (x<y) then
c:=8
else
c:=9
else
if (x=0) or (y=0) then
if(x<0) or (y<0) then
c:=6
else
c:=5
else
if(x<0) or (y<0) then
if (x<y) then
c:=1
else
c:=2
else
if (x<y) then
c:=3
else
c:=4;
SetLength(a,i);
for m:=0 to i-1 do
SetLength(a[m],j);
for m:=0 to i-1 do begin
writeln();
for p:=0 to j-1 do begin
random(c,x,y,a[m,p]); {Заполнить матрицу случайными числами}
if (m<>p) then pro:=pro*a[m,p]; {Вычислить произведение элементов массива, лежащих вне главной диагонали.}
write (a[m,p]:3,' ':3);
end;
end;
writeln('pro=',pro);
a:=NIL;
end.
// ABC Pascal