#include <iostream>
using namespace std;
int main()
{
int num;
cin >> num >> num;
int* ans = new int[num];
for (int jjj = 0; jjj < num; jjj++) cin>> ans[jjj];
for (int jjj = 1; jjj < num; jjj++) {
if (ans[jjj - 1] != ans[jjj] - 1) cout << ans[jjj - 1] << ",";
else {
cout << ans[jjj - 1];
int kkk = jjj;
for (kkk; kkk < num; kkk++)
if (ans[kkk - 1] != ans[kkk] - 1) break;
if (kkk - jjj > 1) cout << "-" << ans[kkk - 1];
else cout << "," << ans[kkk - 1];
jjj = kkk;
if (num - jjj) cout << ",";
}
if (num - jjj == 1) cout << ans[jjj];
// PascalABC.NET 3.1, сборка 1172 от 19.02.2016
begin
Writeln('k=',ArrRandom(15,1,20).Println.Count(x->x>10))
end.
Тестовое решение
4 16 14 9 11 18 17 14 14 15 1 1 9 10 15
k=9
2. А вот так учат решать в школе...
// PascalABC.NET 3.1, сборка 1172 от 19.02.2016
var
a:array[1..15] of integer;
i,k:integer;
begin
Randomize;
k:=0;
for i:=1 to 15 do begin
a[i]:=Random(20)+1;
Write(a[i],' ');
if a[i]>10 then k:=k+1
end;
Writeln;
Writeln('k=',k)
end.
Тестовое решение:
18 15 7 6 3 3 10 6 6 15 2 16 16 2 12
k=6
#include <iostream>
using namespace std;
int main()
{
int num;
cin >> num >> num;
int* ans = new int[num];
for (int jjj = 0; jjj < num; jjj++) cin>> ans[jjj];
for (int jjj = 1; jjj < num; jjj++) {
if (ans[jjj - 1] != ans[jjj] - 1) cout << ans[jjj - 1] << ",";
else {
cout << ans[jjj - 1];
int kkk = jjj;
for (kkk; kkk < num; kkk++)
if (ans[kkk - 1] != ans[kkk] - 1) break;
if (kkk - jjj > 1) cout << "-" << ans[kkk - 1];
else cout << "," << ans[kkk - 1];
jjj = kkk;
if (num - jjj) cout << ",";
}
if (num - jjj == 1) cout << ans[jjj];
}
}