编辑代码

#include <iostream>
#include <string>

using namespace std;

int main() {
    const int ROWS = 10;
    const int COLS = 19; // 每行的字符数
    const int MID = COLS / 2; // 中间列的位置(基于零索引)

    for (int i = 0; i < ROWS; ++i) {
        string row(COLS, ' '); // 初始化为空格的字符串
        int offset = abs(i - ROWS/2); // 行偏移量,计算当前行与中间行的距离

        if (offset <= 4) { // 确保在心形范围内
            int width;
            if (i < ROWS / 2) {
                // 上半部分:宽度随offset线性增加
                width = 2 * offset + 1;
            } else {
                // 下半部分:宽度先不变,然后线性减小
                if (offset <= 2) {
                    width = 9 - 2 * offset; // 达到最大宽度后逐渐变窄
                } else {
                    width = 3; // 最下部分的宽度保持较小
                }
            }

            int left = MID - width / 2;
            int right = MID + width / 2;

            for (int j = left; j <= right; ++j) {
                if (j >=0 && j < COLS) { // 确保索引有效
                    row[j] = '*';
                }
            }

            // 处理心形中间的凹陷部分
            if (offset == 1) {
                row[MID - 3] = ' ';
                row[MID + 3] = ' ';
            } else if (offset == 2) {
                row[MID - 4] = ' ';
                row[MID + 4] = ' ';
            }
        }

        cout << row << endl;
    }

    return 0;
}