【发布时间】:2017-01-15 06:00:57
【问题描述】:
谁能告诉我如何修改我的代码。
我想只使用两个循环打印菱形图案。 如果我输入 5,菱形应该是这样的:
*
***
*****
***
*
我已经完成了一半。
这是我目前得到的。
5
*
***
*****
****
***
下面是我的代码:
#include <iostream>
using namespace std;
// print diamond. Instead of finding * pattern, just find " " 's pattern.
int main()
{
int size;
cin >> size;
int m = size / 2;
for (int row = 0; row < size; row++) {
for (int col = 0; col < size; col++) { // Before || is the left part. After || is right part
if ((row < m && (col < m - row || col > m + row)) || (row > m && col < row - m))
cout << " ";
else
cout << "*";
}
cout << endl;
}
return 0;
}
【问题讨论】:
-
仅供参考,谷歌搜索“stackoverflow c++ diamond loops”会发现很多关于这类事情的现有问题。也许其中一些对你有用。
-
如果是偶数行,比如 6 行,钻石是什么样子的?