如何使用 c++ 打印菱形

How to print diamond shape w/ c++

本文关键字:打印 c++ 何使用      更新时间:2023-10-16

我需要帮助,我不久前创建了一个简短的小程序,它将打印一个带有"*"的简单金字塔,如下所示:

*
***
*****

但我决定挑战自己,看看我是否可以创造出这样一个简单的钻石形状:

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

这是我到目前为止的代码。 我还应该补充一点,您输入的值(例如 5)决定了钻石的大小。

#include <iostream>
#include <sstream>
using namespace std;
int main() {


int value = 0;
cout << "Please enter in a value: ";
cin >> value;
cout << endl;

for (int i = 0; i < value; i++) {
//print spaces v v v
for (int x = 0; x < (value - i - 1); x++) {
cout << " ";
}
//print * v v v
for (int y = 0; y < (2 * i + 1); y++) {
cout << "*";
}
cout << endl;
}
for (int i = 0; i < value; i++) {
int number = 0;
number+= 2;
//print spaces v v v
for (int x = 0; x < (value - value + i + 1); x++) {
cout << " ";
}
//print * v v v
for (int y = 0; y < (/*ATTENTION: What do I do here? Plz help*/); y++) {
cout << "*";
}
cout << endl;
}

return 0;
}

我一直在尝试做的是弄清楚在括号中写着什么(//ATTENTION)。我已经工作了至少一个小时,试图做随机的事情,有一次当我输入 4 时它起作用了,但不是 5,这非常困难。这是构建钻石的关键,尝试输入值并编译以查看会发生什么。我需要它是对称的。

我需要知道在括号里放什么。很抱歉,这很长,但谢谢你的帮助。

如果我的代码混乱且难以阅读,我也深表歉意。

int number = 0;number+= 2;

value - valuefor (int x = 0; x < (value - value + i + 1); x++) {内部

不是必需的。


括号内,您可以使用

2*(value-i-1)-1

但是,我建议您首先分析问题,然后尝试解决问题,而不是尝试随机操作。例如,让我们考虑偶数和奇数输入的情况,即 2 和 3。

偶数大小写 (2)

*
***
***
*

分析之情

Row Index    Number of Spaces    Number of Stars
0            1                   1
1            0                   3
2            0                   3
3            1                   1

对于行索引<值>

  1. 空格数 =value - row index - 1
  2. 星数 =2 * row index + 1

对于行索引>=值空格和星号的数量只是颠倒过来。在奇怪的情况下,情况也类似,只有一小部分例外。

奇数案例 (3)

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

分析之情

Row Index    Number of Spaces    Number of Stars
0            2                   1
1            1                   3
2            0                   5
3            1                   3
4            2                   1

一个小的例外是,在反转时,我们必须忽略行索引 = 值。


现在,如果我们把上面的分析放在代码中,我们得到了解决方案

//Define the Print Function
void PrintDiamond(int rowIndex, int value)
{
//print spaces v v v
for (int x = 0; x < value - rowIndex -1; x++) {
cout << " ";
}
//print * v v v
for (int y = 0; y < 2 * rowIndex + 1; y++) {
cout << "*";
}
cout << endl;
}

然后在主内

//Row index < value
for (int i = 0; i < value; i++) {
PrintDiamond(i,value);
}
//For row index >= value reversing the above case
//value-(value%2)-1 subtracts 1 for even and 2 for odd cases
//ignore the row index = value in odd cases
for (int i = value-(value%2)-1; i >=0; i--) {
PrintDiamond(i,value);
}
#include <iostream>
#include <sstream>
using namespace std;
int main() {
int value = 0;
cout << "Please enter in a value: ";
cin >> value;
cout << endl;

for (int i = 0; i < value; i++) {
//print spaces v v v
for (int x = 0; x < (value - i - 1); x++) {
cout << " ";
}
//print * v v v
for (int y = 0; y < (2 * i + 1); y++) {
cout << "*";
}
cout << endl;
}
for (int i = 0; i < value-1; i++) {
//    int number = 0;
//    number+= 2;
//    //print spaces v v v
for (int x = 0; x < i+1; x++) {
cout << " ";
}
//print * v v v
for (int y = 0; y < (2*(value-1-i)-1); y++) {
cout << "*";
}
cout << endl;
}

return 0;
}

我希望你会得到这个.同样在第二个 for 循环中,您通过将循环迭代到value来额外迭代它一次。但是由于金字塔是对称的,因此金字塔中的行数将2*value-1。所以我在第二个循环中i应该变化到value -1.

此代码应解决此问题:

#include <sstream>
using namespace std;
void printSpaces(int howMany) {
for(int i = 0; i < howMany; i++) cout << " ";
}
void figure(int size) {
bool oddSize = size % 2 == 1;
int center = size / 2;
int spaces = size / 2;
// If figure is of an odd size adjust center
if (oddSize) {
center++;
} else { // Else if figure is of even size adjust spaces
spaces--;
}
for (int i = 1; i <= center; i++) {
printSpaces(spaces);
for(int j = 0; j <  1 + (i - 1) * 2; j++) cout << "*";
cout << endl;
spaces--;
}
spaces = oddSize ? 1 : 0; // If the figure's size is odd number adjust spaces to 1
center -= oddSize ? 1 : 0; // Adjust center if it's an odd size figure
for(int i = center; i >= 1; i--) {
printSpaces(spaces);
for(int j = 0; j <  1 + (i - 1) * 2; j++)
cout << "*";
cout << endl;
spaces++;
}
}
int main() {
int value = 0;
while(value < 3) {
cout << "Please enter in a value (>= 3): ";
cin >> value;
cout << endl;
}
figure(value);
return 0;
}