簡単 0件解決済み 正答率0%

Hollow Square

Draw the outline of a square with * characters.

このチャレンジで学べること

  • 文字で枠を描く
  • 最初と最後の行と列を判定する
  • 1 行ずつ文字列を作る

問題

Draw a hollow square of side size: the first and last rows are all *, and the rows in between have * only at the two ends with spaces inside.

Implement hollow_square(size).

Input

size: The side length.

Output

Return the rows from top to bottom.

制約

1 ≤ size ≤ 50

例

例 1
入力:
size = 4

期待される出力:
expected = ["****", "*  *", "*  *", "****"]
入力:
long long size = 4;

期待される出力:
std::vector<std::string> expected = {"****", "*  *", "*  *", "****"};
入力:
long size = 4;

期待される出力:
string[] expected = new string[] {"****", "*  *", "*  *", "****"};

解説

Rows are "****", "* *", "* *", "****".

例 2
入力:
size = 1

期待される出力:
expected = ["*"]
入力:
long long size = 1;

期待される出力:
std::vector<std::string> expected = {"*"};
入力:
long size = 1;

期待される出力:
string[] expected = new string[] {"*"};

解説

A 1×1 square is a single "*".

解き方のヒント

まずは自分で考えてから、ヒントを 1 つずつ開いてみましょう。

ヒント 1

枠は埋め、内側は空のままにする。

ヒント 2

先頭と末尾の行は、中央の行と分けて扱う。

ヒント 3

サイズ1では1つのセルが枠でも角でもあるので、埋める必要がある。

目安の計算量

時間 O(size^2)、追加メモリ O(size^2)

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。