begin var (n, k) := ReadInteger2('n,k->'); var p := ArrGen(n, t -> t + 1); var m := new integer[k, n div k]; var sum := p.Sum div k; for var i := 0 to k - 1 do begin var m_sum := sum; m[i, 0] := p[0]; m_sum := m_sum - m[i, 0]; p := p[1:]; for var j := 1 to (n div k) - 1 do begin if m_sum - p.Last > 0 then begin m[i, j] := p.max; m_sum := m_sum - p.Max; p := p[:p.IndexMax]; end else begin if p.Where(t -> t = m_sum).Count = 1 then m[i, j] := m_sum; p := p.Where(t -> t <> m_sum).ToArray; end; end; end; var t := true; for var i := 0 to m.RowCount - 2 do if (m.Row(i).sum <> m.Row(i + 1).Sum) or (p.Length <> 0) then begin t := false; break; end; if t then m.Println else 0.Println; end.
#include "stdafx.h" #include <iostream> using namespace std;
int main() { setlocale(LC_ALL, "Rus"); int n; cout << "Введите размер массива-> "; cin >> n; int *mas = new int[n]; int a = 0, b = 0; for (int i = 0; i < n; i++) { cout << "a["<< i << "]="; cin >> mas[i]; if (mas[i] % 2 == 0) a++; else b++; } cout << "Четных= " << a << endl << "Не четных=" << b << endl; system("pause"); }
//Версия 3.3, сборка 1634 (14.02.2018)
begin
var (n, k) := ReadInteger2('n,k->');
var p := ArrGen(n, t -> t + 1);
var m := new integer[k, n div k];
var sum := p.Sum div k;
for var i := 0 to k - 1 do
begin
var m_sum := sum;
m[i, 0] := p[0]; m_sum := m_sum - m[i, 0];
p := p[1:];
for var j := 1 to (n div k) - 1 do
begin
if m_sum - p.Last > 0 then begin m[i, j] := p.max; m_sum := m_sum - p.Max; p := p[:p.IndexMax]; end
else
begin
if p.Where(t -> t = m_sum).Count = 1 then m[i, j] := m_sum;
p := p.Where(t -> t <> m_sum).ToArray;
end;
end;
end;
var t := true;
for var i := 0 to m.RowCount - 2 do
if (m.Row(i).sum <> m.Row(i + 1).Sum) or (p.Length <> 0) then begin t := false; break; end;
if t then m.Println else 0.Println;
end.
Пример(1):
n,k-> 8 2
1 8 7 2
3 6 5 4
Пример(2):
n,k-> 6 3
1 6
2 5
3 4
#include <iostream>
using namespace std;
int main()
{
setlocale(LC_ALL, "Rus");
int n;
cout << "Введите размер массива-> ";
cin >> n;
int *mas = new int[n];
int a = 0, b = 0;
for (int i = 0; i < n; i++) {
cout << "a["<< i << "]="; cin >> mas[i];
if (mas[i] % 2 == 0)
a++;
else
b++;
}
cout << "Четных= " << a << endl << "Не четных=" << b << endl;
system("pause");
}