Программа на паскале: const n=10; var a:array[1..n] of integer; max,min,imax,imin,s,i:integer; begin Randomize; for i:=1 to n do begin a[i]:=random(51); write(a[i],' '); end; writeln; max:=a[1]; imax:=1; min:=a[1]; imin:=1; for i:=1 to n do begin if a[i]>max then begin max:=a[i]; imax:=i; end; if a[i]<min then begin min:=a[i]; imin:=i; end; end; writeln('max = ',max,' min = ',min); s:=0; if imax<imin then for i:=imax to imin do s:=s+a[i] else for i:=imin to imax do s:=s+a[i]; writeln('s = ',s); end.
Пример: 38 15 4 5 9 15 24 40 12 9 max = 40 min = 4 s = 97
#include <iostream>
// матрица 2 на 2// a b// c dtemplate< typename T = int >class matrix2{public: T a, b, c, d;
matrix2() : a(0), b(0), c(0), d(0) {} matrix2(T a, T b, T c, T d) : a(a), b(b), c(c), d(d) {}
matrix2 & operator *= (matrix2 & other) { T ta, tb, tc, td; ta = a * other.a + b * other.c; tb = a * other.b + b * other.d; tc = a * other.c + c * other.d; td = b * other.c + d * other.d; a = ta, b = tb, c = tc, d = td; }
matrix2 operator * (matrix2 & other) { T ta, tb, tc, td; ta = a * other.a + b * other.c; tb = a * other.b + b * other.d; tc = a * other.c + c * other.d; td = b * other.c + d * other.d; return matrix2(ta, tb, tc, td); }
matrix2 pow(int power) { matrix2 result(1, 0, 0, 1); matrix2 cur = *this; while (power) { if (power & 1) { power ^= 1; result *= cur; } else { power >>= 1; cur *= cur; } } return result; }
void operator = (matrix2 other) { a = other.a; b = other.b; c = other.c; d = other.d; }
friend std::ostream & operator << (std::ostream & ostr, matrix2 ma) { ostr << std::endl; ostr << ma.a << " " << ma.b << std::endl; ostr << ma.c << " " << ma.d << std::endl; return ostr; }
};
int main(void){ matrix2< int > m1(1, 1, 1, 0), tmp;
const int N = 12;
std::cout << m1.pow(N).a;
return 0;}
const n=10;
var a:array[1..n] of integer;
max,min,imax,imin,s,i:integer;
begin
Randomize;
for i:=1 to n do begin
a[i]:=random(51);
write(a[i],' ');
end;
writeln;
max:=a[1]; imax:=1;
min:=a[1]; imin:=1;
for i:=1 to n do
begin
if a[i]>max then begin max:=a[i]; imax:=i; end;
if a[i]<min then begin min:=a[i]; imin:=i; end;
end;
writeln('max = ',max,' min = ',min);
s:=0;
if imax<imin
then for i:=imax to imin do s:=s+a[i]
else for i:=imin to imax do s:=s+a[i];
writeln('s = ',s);
end.
Пример:
38 15 4 5 9 15 24 40 12 9
max = 40 min = 4
s = 97