max = a[0]+a[n-1]; min = a[0]*a[n/2]; for (int i = 1; i<n/2; i++){ if (a[i]+a[n-i-1] > max) max = a[i]+a[n-i-1]; if (a[i]*a[n/2+i] < min) min = a[i]*a[n/2+i]; } cout << "max = " << max << "\n"; cout << "min = " << min << "\n"; return 0; }
Пример (для n=10): -19.29 30.99 -15.37 26.41 3.65 13.45 29.28 4.28 30.51 -10.36 max = 61.50 min = -259.43
a: array[0..10] of integer;
i, n, k: integer;
begin
for i := 0 to 10 do
A[i] := i + 1;
for i := 0 to 10 do
begin
A[i] := A[10 - i];
PrintLn(a);
end;
end.
Трасировка А
[11,2,3,4,5,6,7,8,9,10,11]
[11,10,3,4,5,6,7,8,9,10,11]
[11,10,9,4,5,6,7,8,9,10,11]
[11,10,9,8,5,6,7,8,9,10,11]
[11,10,9,8,7,6,7,8,9,10,11]
[11,10,9,8,7,6,7,8,9,10,11]
[11,10,9,8,7,6,7,8,9,10,11]
[11,10,9,8,7,6,7,8,9,10,11]
[11,10,9,8,7,6,7,8,9,10,11]
[11,10,9,8,7,6,7,8,9,10,11]
[11,10,9,8,7,6,7,8,9,10,11]
ответ 3
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
const int n = 30;
double a[n];
double max, min;
srand(time(0));
for (int i = 0; i < n; i++) {
a[i]=-20 + (51.0 / RAND_MAX) * rand();
cout << fixed << setw (7) << setprecision (2) << a[i];
}
cout <<"\n";
max = a[0]+a[n-1];
min = a[0]*a[n/2];
for (int i = 1; i<n/2; i++){
if (a[i]+a[n-i-1] > max) max = a[i]+a[n-i-1];
if (a[i]*a[n/2+i] < min) min = a[i]*a[n/2+i];
}
cout << "max = " << max << "\n";
cout << "min = " << min << "\n";
return 0;
}
Пример (для n=10):
-19.29 30.99 -15.37 26.41 3.65 13.45 29.28 4.28 30.51 -10.36
max = 61.50
min = -259.43