问题描述
我想要的输出看起来像这样:
** ** ** ** **
**** **** **** **** ****
****** ****** ****** ****** ******
******** ******** ******** ******** ********
********** ********** ********** ********** **********
********** ********** ********** ********** **********
******** ******** ******** ******** ********
****** ****** ****** ****** ******
**** **** **** **** ****
** ** ** ** **
** ** ** ** **
**** **** **** **** ****
****** ****** ****** ****** ******
******** ******** ******** ******** ********
********** ********** ********** ********** **********
********** ********** ********** ********** **********
******** ******** ******** ******** ********
****** ****** ****** ****** ******
**** **** **** **** ****
** ** ** ** **
** ** ** ** **
**** **** **** **** ****
****** ****** ****** ****** ******
******** ******** ******** ******** ********
********** ********** ********** ********** **********
********** ********** ********** ********** **********
******** ******** ******** ******** ********
****** ****** ****** ****** ******
**** **** **** **** ****
** ** ** ** **
//inside of a void function called printDots()
char dot = '*';
char blank = ' ';
for (int i = 1; i < 6; i++) {
for (int k = 6; k > i; k--) { // right-shifts the first triangle
std::cout << blank;
}
for (int j = 0; j < i; j++) { // first triangle
std::cout << dot;
}
for (int j = 0; j < i; j++) { // second triangle (left-aligned)
std::cout << dot;
}
std::cout << "\n";
}
for (int i = 1; i < 6; i++) {
for (int k = 0; k < i; k++) { // right-shifts the third triangle
std::cout << blank;
}
for (int j = 6; j > i; j--) { // third triangle
std::cout << dot;
}
for (int j = 6; j > i; j--) { // fourth triangle (left-aligned)
std::cout << dot;
}
std::cout << "\n";
}
不过,我正在努力弄清楚如何以所需的模式重复此形状。我已经尝试添加更多 for
循环来做与我已经在做的相同的事情来制作钻石的所有四个部分,但它似乎只是将星号垂直向下移动并破坏模式,所有这些都没有实现任何水平输出。我可以使用大 std::cout
语句手动输出模式,但我试图弄清楚如何生成形状而不是仅仅复制和粘贴它。有什么建议吗?
解决方法
我会重新设计您的应用程序,以便您拥有打印一行钻石的功能:
所以最初的程序是这样的:
void printDiamondLine(int line)
{
// You write this part
}
int main()
{
for(int loop = 0; loop < sizeOfDiamong; ++loop)
{
printDiamondLine(loop);
std::cout << "\n";
}
}
这应该打印出来:
** Notice that each line is space filled on both sides.
****
******
********
**********
**********
********
******
****
**
现在您可以更改代码以打印多个直径:
int main()
{
// Looping over the whole things
// to get multiple lines of diamonds.
for(int vertical = 0; vertical < verticalDiamonds; ++vertical)
{
for(int loop = 0; loop < sizeOfDiamong; ++loop)
{
// Printing each line of the diamond
// multiple times across.
for(int horz = 0; horz < horzDiamonds; ++horz)
{
printDiamondLine(loop);
std::cout << " ";
}
std::cout << "\n";
}
}
}
,
您必须一次将其视为 1 行。您的第一个大 foo 循环生成每个菱形的上半部分,第二个大 for 循环生成下半部分,是吗?
我只处理其中的一半,因为它从顶部和底部变成了相同的解决方案:
// This produces 5 lines of output.
for (int i = 1; i < 6; i++) {
// This puts out leading spaces as required for this line
for (int k = 6; k > i; k--) { // right-shifts the first triangle
std::cout << blank;
}
// This puts out the diamond itself -- just this line
for (int j = 0; j < i; j++) { // first triangle
std::cout << dot;
}
// And this puts out the second half of the diamond -- just this line
for (int j = 0; j < i; j++) { // second triangle (left-aligned)
std::cout << dot;
}
// Note: THIS IS WHERE WE'LL INSERT MORE CODE
// And this is why you're only getting 1
std::cout << "\n";
}
我注释了代码。到目前为止,你实际上做得很好。但是现在,让我们保留所有代码,但我将在最终 cout 之前添加一点。
for (int k = 6; k > i; k--) {
std::cout << blank;
}
这样做是用空格填充行,以便(到目前为止)每行的长度相同。
然后你所要做的就是将大部分代码包装在另一个 for 循环中。你最终会得到这样的结果:
for (int i = 1; i < 6; i++) {
for (int whichDiamond = 0; whichDiamond < 3; ++whichDiamond) {
for (int k = 6; k > i; k--) { // right-shifts the first triangle
std::cout << blank;
}
for (int j = 0; j < i; j++) { // first triangle
std::cout << dot;
}
for (int j = 0; j < i; j++) { // second triangle (left-aligned)
std::cout << dot;
}
for (int k = 6; k > i; k--) { // right-shifts the first triangle
std::cout << blank;
}
}
std::cout << "\n";
}
你可以用第二个大循环做同样的事情。
现在,可能有更聪明的方法来做到这一点,但这适用于您现有的代码。