1 条题解
-
0
练38.1 宇宙大爆炸
解题思路
第1秒边长1,每增加1秒四周扩一圈,边长增加2,因此第n秒是边长2n-1的实心正方形。
正确性说明
计算规则直接来自题目定义。按所述顺序处理时,已完成部分的结果被准确保存在累加量、计数或当前状态中;下一步只加入新增贡献。最终得到完整要求的结果。
易错点
行数与每行星号数都为2n-1。
复杂度
时间O(n²),空间O(n)。
参考代码(C++11)
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; int w = 2 * n - 1; for (int i = 0; i < w; i++) cout << string(w, '*') << "\n"; return 0; }
- 1