如何使用 c++ 打印菱形
How to print diamond shape w/ c++
我需要帮助,我不久前创建了一个简短的小程序,它将打印一个带有"*"的简单金字塔,如下所示:
*
***
*****
但我决定挑战自己,看看我是否可以创造出这样一个简单的钻石形状:
*
***
*****
***
*
这是我到目前为止的代码。 我还应该补充一点,您输入的值(例如 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 - value
for (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
对于行索引<值>值>
- 空格数 =
value - row index - 1
- 星数 =
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;
}
- 如何循环打印顶点结构
- 为什么在popback()操作之后,它仍然打印完整的矢量
- 如何在c++中打印目录
- 有一个打印语句的函数是一种糟糕的编程实践吗
- 在线编译器中的分段C++没有打印消息
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 如何将结构插入到集合中并打印集合的成员
- 在循环C++中指定字符串之后,不会打印该字符串
- 以螺旋方式打印矩阵的程序.(工作不好)
- 从控制台中删除最后打印的元素
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 如何仅使用对象名称打印特定于对象的成员
- 回溯C++不打印函数,因此文件
- 在一定长度后从数组中打印时缺少整数
- 为什么这个 c++ 代码打印出长度 5,当我打印出字符串时,程序会自动终止?
- 在gem5中打印文件中的所有cache_blocks
- 打印数字图案
- Log4cpp:以UTC/GMT时区打印日期
- 如何使用gdb制作一个可以漂亮地打印每个对象的C++函数