在屏幕上打印 X 的形状
Print the shape of an X on screen
我想像这样在屏幕上打印一个X:
* *
* *
* *
*
* *
* *
* *
我尝试使用此代码:
int main(){
bool back = false;
for (int i = 0; i < 7; ++i) {
if (i == 4)
back = true;
if (!back){
for (int j = 0; j < i; ++j) {
cout << " ";
}
} else{
for (int j = 7-i-1; j > 0; --j) {
cout << " ";
}
}
cout << "*" << endl;
}
}
结果缺少右半部分:
*
*
*
*
*
*
*
问题是我无法弄清楚如何打印星星和跟随它们的星星之间的空间。
解决此问题的更具教育性的方法需要 2 个循环。
第一个for
循环控制输出的高度,即打印的行数。每次迭代打印一行并以std::endl
结束。
第二个是嵌套for
循环,它控制宽度并水平打印字符,即它打印该行的星号和空格。每次迭代打印一个空格或一个星号。
此图可能有助于理解变量的值,当x_size = 5
:
(width)
0 1 2 3 4
(height) ---------------------
0 | * | | | | * | asterisk_pos = 0, end_pos = 4, inc = 1
---------------------
1 | | * | | * | | asterisk_pos = 1, end_pos = 3, inc = 1
---------------------
2 | | | * | | | asterisk_pos = 2, end_pos = 2, inc = 1
---------------------
3 | | * | | * | | asterisk_pos = 1, end_pos = 3, inc = -1
---------------------
4 | * | | | | * | asterisk_pos = 0, end_pos = 4, inc = -1
---------------------
源代码:
int main()
{
int x_size = 7; // size of the drawing
int asterisk_pos = 0; // initial position of the asterisk
int inc = 1; // amount of increment added to asterisk_pos after an entire line has been printed
// height is the line number
for (int height = 0; height < x_size; height++)
{
// width is the column position of the character that needs to be printed for a given line
for (int width = 0; width < x_size; width++)
{
int end_pos = (x_size - width) - 1; // the position of the 2nd asterisk on the line
if (asterisk_pos == width || asterisk_pos == end_pos)
cout << "*";
else
cout << " ";
}
// print a new line character
cout << std::endl;
/* when the middle of x_size is reached,
* it's time to decrease the position of the asterisk!
*/
asterisk_pos += inc;
if (asterisk_pos > (x_size/2)-1)
inc *= -1;
}
return 0;
}
带x_size = 7
输出:
* *
* *
* *
*
* *
* *
* *
带x_size = 3
输出:
* *
*
* *
观察每行中的序列。看看你拥有的第一部分:
- 0 空格、1 *、5 空格、
- 1 *、0 空格 1 个空格、1 *、3 个空格、1
- *、1 个空格 2 个空格、1 *、1 个空格、
- 1 *、2 个空格
5-2 i 空格,后跟 1 *,后跟 i 空格
那么以下内容应该有效:
for (int line=0; line<3; line++) {
for (int n=0; n<line; n++) cout << ' ';
cout << '*';
for (int n=0; n<5-2*line; n++) cout << ' ';
cout << '*';
for (int n=0; n<line; n++) cout << ' ';
cout << endl;
}
中间线3很明显,下面是第一部分的反面。
另一种方法是观察 * 的位置顺序:(0,6( (1,5( (2,4( (3,3( (4,2( (5,1( (6,0(,因此:
for (int line=0; line<7; line++) {
int pos1 = line;
int pos2 = 6-line;
for (int n=0; n<7; n++) {
if (n==pos1 || n==pos2) cout << '*';
else cout << ' ';
}
cout << endl;
}
然后,您可以明显地删除pos1
并pos2
...
上
部之间的空格减少 2 并以 line - 2
开头
下部之间的空格被激怒了 2
这是我如何解决你的问题
void printSpaces(int count)
{
for (int i = 0; i < count; ++i) {
cout << " ";
}
}
int main()
{
int lines = 7;
int spaceBefore = 0;
int spaceBetween = lines - 2;
bool backword = false;
for (int i = 0; i < lines; ++i)
{
printSpaces(spaceBefore);
cout << "*";
if (spaceBetween > 0)
{
printSpaces(spaceBetween);
cout << "*";
}
else
{
backword = true;
}
cout << "n";
spaceBefore = backword ? spaceBefore-1 : spaceBefore+1;
spaceBetween = backword ? spaceBetween+2 : spaceBetween-2;
}
return 0;
}
该模式由两个等式组成:x = y
和 x + y = 4
只需循环遍历轴并绘制落在任何一条线上的点即可。
( y )
0 1 2 3 4
( x ) ---------------------
0 | * | | | | * |
---------------------
1 | | * | | * | |
---------------------
2 | | | * | | |
---------------------
3 | | * | | * | |
---------------------
4 | * | | | | * |
---------------------
Two Equations
x = y
x + y = 4
#include <iostream>
int main() {
int num_lines = 7;
auto on_line1 = [](int x, int y) {
return x == y;
};
auto on_line2 = [num_lines](int x, int y) {
return (x + y) == (num_lines - 1);
};
for(int x = 0; x < num_lines; x++) { // Simple looping
for(int y = 0; y < num_lines; y++) { // through the axes
if(on_line1(x, y) or on_line2(x, y)) { // If on any of the line
std::cout << '*'; // Then plot it
} else {
std::cout << ' '; // Else leave it
}
}
std::cout << 'n';
}
return 0;
}
PS:我从另一个答案中复制了ascii表。
如果您不需要循环,则可以创建一个string
并打印它。
#include <iostream>
#include <string>
int main(int argc, char * argv[]){
std::string myX("* *n * * n * * n * n * * n * * n* *n");
std::cout << myX;
return 0;
}
相关文章:
- 使用 SFML 和 C++ 将 Pixel 打印到屏幕上
- 使用 fstream 库并在屏幕上打印的文件有问题?
- UE4在屏幕上的打印位置
- Cout 一个字符串可以打印任何内容到屏幕上
- 如何在多维矢量中仅添加特定元素并将内容打印到屏幕
- 如何以CV_16UC1的方式读取JPG图像并在屏幕上打印?
- UNIX API 调用:使用 read() 函数打开文件并将其打印到屏幕上会增加额外的随机字符
- 作为参数传递的打印功能指针导致屏幕上打印"1"
- 在屏幕上打印 X 的形状
- 如何读取二进制文件并在屏幕上打印 3?
- 用putchar在屏幕上打印字符
- C / SFML:使用两个递归调用在屏幕上打印凸形形状仅显示第一个递归调用中的形状,而不是第二个
- 如何在不保存文件的情况下制作打印屏幕并将其发送到FTP服务器?我的工作代码将文件保存到HDD
- 如何在屏幕上打印2^20行C 的整数(1秒钟以下)
- 使用freopen()打印到文件和屏幕
- C++ 强制 std::cout 刷新(打印到屏幕)
- 无法在屏幕上打印 nullptr 的值
- 如何使用Qt/C++和各种显示器拍摄打印屏幕
- C++以二进制形式读取整个文件并将其打印在屏幕上
- Qt:按键事件中的打印屏幕键