C++ 在屏幕上打印X的形状
我想在屏幕上打印一个X,如下所示:C++ 在屏幕上打印X的形状,c++,C++,我想在屏幕上打印一个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 &l
* *
* *
* *
*
* *
* *
* *
我尝试使用以下代码:
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;
}
}
问题是我不知道如何打印星星之间的空格和紧随其后的星星。观察每行的顺序。看看你的第一部分: 0个空间,1*,5个空间,1*,0个空间 1个空格,1*,3个空格,1*,1个空格 2个空间,1*,1个空间,1*,2个空间 然后对于第i行:i个空间后跟1*后跟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…观察每行中的顺序。看看你的第一部分: 0个空间,1*,5个空间,1*,0个空间 1个空格,1*,3个空格,1*,1个空格 2个空间,1*,1个空间,1*,2个空间 然后对于第i行:i个空间后跟1*后跟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,并从第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;
}
上半部分中的间距减少2,并从第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;
}
解决此问题的更具教育意义的方法需要两个循环 第一个for循环控制输出的高度,即打印的行数。每次迭代都打印一行,并以std::endl结束 第二个是嵌套for循环,它控制宽度并水平打印字符,即为该行打印星号和空格。每次迭代打印一个空格或一个星号 此图可能有助于理解x_size=5时变量的值: 源代码: x_大小为3的输出:
解决此问题的更具教育意义的方法需要两个循环 第一个for循环控制输出的高度,即打印的行数。每次迭代都打印一行,并以std::endl结束 第二个是嵌套for循环,它控制宽度并水平打印字符,即为该行打印星号和空格。每次迭代打印一个空格或一个星号 此图可能有助于理解x_size=5时变量的值: 源代码: x_大小为3的输出:
如果不需要循环,可以创建一个字符串并打印它
#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;
}
如果不需要循环,可以创建一个字符串并打印它
#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;
}
该模式由两个方程组成:x=y和x+y=4 只需在轴上循环并绘制落在任何直线上的点
( y )
0 1 2 3 4
( x ) ---------------------
0 | * | | | | * |
---------------------
1 | | * | | * | |
---------------------
2 | | | * | | |
---------------------
3 | | * | | * | |
---------------------
4 | * | | | | * |
---------------------
Two Equations
x = y
x + y = 4
PS:我从中复制了ascii表 模式由两个方程组成:x=y和x+y=4 只需在轴上循环并绘制落在任何直线上的点
( y )
0 1 2 3 4
( x ) ---------------------
0 | * | | | | * |
---------------------
1 | | * | | * | |
---------------------
2 | | | * | | |
---------------------
3 | | * | | * | |
---------------------
4 | * | | | | * |
---------------------
Two Equations
x = y
x + y = 4
PS:我从中复制了ascii表 这个技巧看起来也不错:@Asesh不是我的第二个解决方案吗?这个技巧看起来也不错:@Asesh不是我的第二个解决方案吗?
#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;
}