Легко Решено: 0 Процент решений: 0%

X Pattern

Draw both diagonals of an n by n square with stars to form an X.

Чему вы научитесь

  • Recognise the two diagonals of a square by their coordinates
  • Build each row cell by cell
  • Trim trailing spaces from a row

Условие

Показать перевод

Picture an n by n grid with rows and columns numbered from 0. The cell in row r and column c holds * when it lies on either diagonal, that is when c = r or c = n − 1 − r; every other cell is a space. Then each row has its trailing spaces removed. For odd n the diagonals cross in a single centre cell; for even n they pass between cells.

Implement x_pattern(n).

Input

n: The side length of the square.

Output

Return the n rows from top to bottom, without trailing spaces.

Ограничения

0 ≤ n ≤ 100

Примеры

Пример 1
Ввод:
n = 5

Ожидаемый вывод:
expected = ["*   *", " * *", "  *", " * *", "*   *"]
Ввод:
long long n = 5;

Ожидаемый вывод:
std::vector<std::string> expected = {"*   *", " * *", "  *", " * *", "*   *"};
Ввод:
long n = 5;

Ожидаемый вывод:
string[] expected = new string[] {"*   *", " * *", "  *", " * *", "*   *"};

Пояснение

Row 0 has stars at columns 0 and 4, row 2 only at column 2: ["* *", " * *", " *", " * *", "* *"].

Пример 2
Ввод:
n = 4

Ожидаемый вывод:
expected = ["*  *", " **", " **", "*  *"]
Ввод:
long long n = 4;

Ожидаемый вывод:
std::vector<std::string> expected = {"*  *", " **", " **", "*  *"};
Ввод:
long n = 4;

Ожидаемый вывод:
string[] expected = new string[] {"*  *", " **", " **", "*  *"};

Пояснение

Row 1 has stars at columns 1 and 2, which sit side by side: ["* *", " **", " **", "* *"].

Как подступиться

Сначала попробуйте сами, затем открывайте подсказки по одной.

Подсказка 1

The main diagonal is where the row equals the column.

Подсказка 2

The other diagonal is where the row and column add up to n − 1.

Подсказка 3

Remove trailing spaces after building each row.

Ожидаемая сложность

O(n^2) time, O(n^2) extra space

Ссылки

Это задание адаптировано для CodeUtility. Следуйте условию и ограничениям выше.

Лучшее принятое решение каждого участника. Более быстрые, экономные и высоко оценённые решения стоят выше.

Принятых решений пока нет. Решите задачу и займите первое место.

Учитесь у других

Решения сообщества

Пока нет опубликованных решений. Решите задание, чтобы поделиться первым.