random_device rd;
mt19937 eng(rd());
uniform_int_distribution<int> dist(1, 10);
int n;
int x;
cin >> n >> x;
vector<int> a(n);
for (auto& i : a)
i = dist(eng);
copy(a.cbegin(), a.cend(), ostream_iterator<int>(cout, " "));
cout << '\n';
sort(a.begin(), a.end());
copy(a.cbegin(), a.cend(), ostream_iterator<int>(cout, " "));
cout << '\n';
if (binary_search(a.cbegin(), a.cend(), x)) {
cout << x << " found";
} else {
set<int> temp(a.cbegin(), a.cend());
if (x < *temp.cbegin()) {
cout << *temp.cbegin();
} else if (x > *prev(temp.cend())) {
cout << *prev(temp.cend());
} else {
auto f = lower_bound(a.cbegin(), a.cend(), x);
auto s = upper_bound(a.cbegin(), a.cend(), x);
if (f != a.cend() && s != a.cend()) {
if (abs(*f - x) < abs(*s - x)) {
cout << *f;
} else {
cout << *s;
}
} else if (f == a.cend() && s != a.cend()) {
cout << *s;
} else if (f != a.cend() && s == a.cend()) {
cout << *f;
}
}
}
const max=10;
var a: array[1..max,1..max] of integer;
k1,k2: array[1..max]of byte;
n,m,i,j,s1,s2: integer;
begin
repeat
write('количество строк до ',max,' n=');
readln(n);
until n in [1..max];
repeat
write('количество столбцов до ',max,' m=');
readln(m);
until m in [1..max];
writeln('исходная матрица: ');
for i: =1 to n do
begin
for j: =1 to m do
begin
a[i,j]: =-15+random(31);
write(a[i,j]: 4);
end;
writeln;
end;
s1: =0;
s2: =0;
for j: =1 to m do
begin
k1[j]: =0;
k2[j]: =0;
for i: =1 to n do
if abs(a[i,j])> 10 then
begin
a[i,j]: =10;
inc(s1);
inc(k1[j]);
end
else
begin
a[i,j]: =j;
inc(s2);
inc(k2[j]);
end;
end;
writeln('измененная матрица');
for i: =1 to n do
begin
for j: =1 to m do
write(a[i,j]: 4);
writeln;
end;
writeln(' количество замен');
writeln(' 1 вида 2 вида');
for i: =1 to m do
writeln('столбец ',i: 2,k1[i]: 5,k2[i]: 5);
writeln('всего ',s1: 5,s2: 5);
end.
Поделитесь своими знаниями, ответьте на вопрос: