#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];
Код для питона:
def solve(a):
def count(r,c):
return a[r][c] if r >= 0 and r < len(a) and c >= 0 and c < len(a[r]) and a[r][c] != None else 0
a[0][0] = 1
for step in range(1, len(a) + len(a[0]) - 1):
row, col = (0, step) if step < len(a[0]) else (step - len(a[0]) + 1, len(a[0]) - 1)
while col >= 0 and row < len(a):
if a[row][col] != None:
a[row][col] = count(row-1, col) + count(row, col-1)
row, col = row+1, col-1
return a[-1][-1]
Поле задаём в виде списка списков:
field = [
[0,0,0,0,0,None,None,None],
[0,0,None,0,0,0,0,None],
[0,0,0,0,0,0,0,0],
[0,None,0,0,0,0,0,0],
[0,0,0,0,None,0,0,0],
[None,None,0,0,0,None,0,0],
[None,None,0,0,0,0,0,0]
]
Объяснение:
Если нужно просто решение, то я приложил картинку
#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];
}
}
Код для питона:
def solve(a):
def count(r,c):
return a[r][c] if r >= 0 and r < len(a) and c >= 0 and c < len(a[r]) and a[r][c] != None else 0
a[0][0] = 1
for step in range(1, len(a) + len(a[0]) - 1):
row, col = (0, step) if step < len(a[0]) else (step - len(a[0]) + 1, len(a[0]) - 1)
while col >= 0 and row < len(a):
if a[row][col] != None:
a[row][col] = count(row-1, col) + count(row, col-1)
row, col = row+1, col-1
return a[-1][-1]
Поле задаём в виде списка списков:
field = [
[0,0,0,0,0,None,None,None],
[0,0,None,0,0,0,0,None],
[0,0,0,0,0,0,0,0],
[0,None,0,0,0,0,0,0],
[0,0,0,0,None,0,0,0],
[0,0,0,0,0,0,0,0],
[None,None,0,0,0,None,0,0],
[None,None,0,0,0,0,0,0]
]
Объяснение:
Если нужно просто решение, то я приложил картинку