并排打印两颗钻石

print two diamonds side by side

我想并排打印两颗钻石,但我的代码打印了 1 颗钻石。

我花了很多时间在上面,真的不知道还能做什么。

如有任何帮助,我们将不胜感激。

#include<iostream>
#include<math.h>
using namespace std;

int main()
{
int i,j,n, middle, spaceCount, starCount;
cin >> n;

middle = (n - 1) / 2;

for ( i = 0; i < n; i++)
{
    spaceCount = abs(middle - i);
    starCount = n - 2 * abs(middle - i);

        for ( j = 0; j < spaceCount; j++)
            cout << " ";
       
        for (j = 0; j < starCount; j++)
            cout << "*";

        for (j = 0; j < spaceCount; j++)
            cout << " ";

            cout << endl;
}

}

输入=奇数

期望的输出=

  *    *
 ***  ***
**********
 ***  ***
  *    *

您忘记打印第二个菱形。每次迭代,你应该首先打印几个空格,然后是星星,然后是双倍的空格(完成第一个钻石并为第二个钻石设置空间)然后你应该为你的第二个钻石画星星。 这是打印两个钻石的代码示例:

#include<iostream>
#include<math.h>
using namespace std;

int main()
{
int i,j,n, middle, spaceCount, starCount;
cin >> n;

middle = (n - 1) / 2;

for ( i = 0; i < n; i++)
{
    spaceCount = abs(middle - i);
    starCount = n - 2 * abs(middle - i);

        // print a row of the first diamonds
        // spaces at the beginning
        for ( j = 0; j < spaceCount; j++)
            cout << " ";
       
        // the stars itself
        for (j = 0; j < starCount; j++)
            cout << "*";

        // finish the rectangle of the first diamond
        for (j = 0; j < spaceCount; j++)
            cout << " ";
        
        // print a row of the second diamond
        // spaces at the beginning
        for (j = 0; j < spaceCount; j++)
            cout << " ";

        // the stars itself
        for (j = 0; j < starCount; j++)
            cout << "*";
  
        // spaces at the end are not necessarily required for the last diamond

        cout << endl;
}

}

最好创建一个函数来打印一个钻石(行)并调用此函数两次(这样可以防止重复代码)。