Solved FillDiags

This commit is contained in:
Leonid
2025-04-26 16:22:42 +03:00
parent 29e9d3247d
commit b1682b8cad

View File

@@ -0,0 +1,63 @@
package com.algos.stepik;
//У вас есть переменная n, которая содержит входные пользовательские данные.
//
// Напишите код, который создает двумерный список и заполняет его по следующему правилу: на главной диагонали должны быть записаны числа 0. На двух диагоналях, прилегающих к главной, числа 1. На следующих двух диагоналях числа 2, и т.д.
//
// Результат записать в виде нового списка в переменную result.
//
// Например:
//
// Если n = 4 тогда:
//
// [
// [ 0, 1, 2, 3 ],
// [ 1, 0, 1, 2 ],
// [ 2, 1, 0, 1 ],
// [ 3, 2, 1, 0 ]
// ]
//
// Если n = 5 тогда:
//
// [
// [ 0, 1, 2, 3, 4 ],
// [ 1, 0, 1, 2, 3 ],
// [ 2, 1, 0, 1, 2 ],
// [ 3, 2, 1, 0, 1 ],
// [ 4, 3, 2, 1, 0 ]
// ]
import com.google.gson.Gson;
import java.util.*;
public class FillDiags {
public static void main(String[] args) {
//int n = readInput();
int n = 5;
List<List<Integer>> result = fillDiagonalList(n);
System.out.println(new Gson().toJson(result));
}
public static List<List<Integer>> fillDiagonalList(int n) {
List<List<Integer>> result = new ArrayList<>();
for (int i = 0; i < n; i++) {
List<Integer> row = new ArrayList<>();
for (int j = 0; j < n; j++) {
row.add(Math.abs(i - j));
}
result.add(row);
}
return result;
}
public static int readInput() {
Scanner scanner = new Scanner(System.in);
return Integer.parseInt(scanner.nextLine().trim());
}
}