C++ 使用星号(*)绘制 X 字母形状
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10086620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
draw X letter shape using asterisk(*)
提问by TTT
i want to write a program to draw the shape of X letter using asterisk(*)
我想编写一个程序来使用星号(*)绘制X字母的形状
#include "stdafx.h"
#include <iostream>
using namespace std;
int main(int argc, char* argv[]){
int i, j;
for(i = 1; i <= 9; i++){
for(j = 1; j <= 12; j++){
if(i == j){
cout << "***";
}else{
cout << " ";
}
}
cout<< endl;
}
return 0;
}
i am very new to programming
我对编程很陌生
i only made (\) how can I make the whole X
我只做了 (\) 我怎么能做整个 X
***------***
-***----***-
--***--***--
---******---
--***--***--
-***----***-
***------***
that's what i did uptill now
这就是我到现在为止所做的
include "stdafx.h"
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
int i, d, a=1,b=12,c;
for(i = 1; i <= 6; i++)
{
for (d=1; d<i;d++) {cout <<" ";}
cout<<"***";
for (c=a+1; c<b;c++) {cout <<" ";}
{cout<<"***";}
for(b=12-i;b<i;b++)
{cout<<"***";}
cout<<endl;
a++;
}
return 0;
}
i divided the top of the (\//) to three parts [space][][space][]
我将 (\//) 的顶部分成三部分 [space][ ][space][]
回答by Ahamed
I have written the following function/method in java. You can convert it to c++;
我在java中编写了以下函数/方法。你可以把它转换成c++;
public static void printX(int x) {
char[] chars = new char[x];
for (int i = 0; i < x; i++) {
chars[i] = '*';
chars[x - 1 - i] = '*';
for (int j = 0; j < x; j++) {
if (j == i || j == (x - 1 - i)) {
continue;
}
chars[j] = ' ';
}
System.out.println(new String(chars));
}
}
If you call the above function/method as printX(5); The output will by 5x5 sized and containing X character.
如果您将上述函数/方法称为 printX(5); 输出将按 5x5 大小并包含 X 字符。
* *
* *
*
* *
* *
回答by Zain Khan
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
Firstly, pardon my very uneven X. For you as a beginner I would give out an algofor you to ponder upon rather than spoon feeding a code.
首先,请原谅我非常不平衡的 X。对于作为初学者的您,我会给出一个算法供您思考而不是用勺子喂代码。
The interpreter does not know how to come back to a line which has already been printer. Therefore, you would have to draw both sides of the X in one iteration of the loop.
After that you decrease you star count (star--) and draw line # 2.
Repeat till the mid way mark when your stars are 0.
When your code sees that the stars are 0, then start with the same loop but this time star++ in each iteration.
This is repeated till the starting count of the star i.e 4 in my case.
解释器不知道如何回到已经打印过的行。因此,您必须在循环的一次迭代中绘制 X 的两侧。
之后,您减少星数(star--)并绘制线#2。
当你的星星为 0 时,重复直到中间标记。
当您的代码看到星星为 0 时,则以相同的循环开始,但这次是在每次迭代中使用 star++。
重复此操作直到星星的起始计数,即在我的情况下为 4。
If any problems you can post your code on the site :)
如果有任何问题,您可以在网站上发布您的代码:)
回答by Spook
You shall dynamically evaluate the spacing areas in each row. Draw manually desired shape on piece of paper and try to create function, which takes as an argument the row number and returns amount of spaces required in the specific row. For example:
您应动态评估每行中的间距区域。在纸上手动绘制所需的形状并尝试创建函数,该函数将行号作为参数并返回特定行中所需的空格量。例如:
* *
* *
*
* *
* *
The amount of spaces in each row equals:
每行中的空格数等于:
0 [*] 3 [*]
1 [*] 1 [*]
2 [*]
1 [*] 1 [*]
0 [*] 3 [*]
Note, that inside each row you'll need two loops: first for the initial and middle spaces.
请注意,在每一行中,您需要两个循环:首先用于初始和中间空格。
回答by MSalters
The solution I wrote 2 decades ago (when I was still learning):
我20年前写的解决方案(当时我还在学习):
- Make an line-column array, e.g.
char screen[80][25];
- Clear it by setting all entries to
' '
- "Draw a point" at x,y by setting
screen[x][y]='*';
- When done, renderthe whole
screen[80][25]
by callingcout
2080 times. (2000 times for the characters and and 80 times forendl
)
- 制作一个行列数组,例如
char screen[80][25];
- 通过将所有条目设置为清除它
' '
- 通过设置在 x,y 处“绘制一个点”
screen[x][y]='*';
- 完成后,通过调用2080 次来渲染整体。(字符 2000 次和 80 次)
screen[80][25]
cout
endl
In your case, you know how to draw a \
. You can easily adapt this. But with my method, you can then draw a /
in the same screen
array. And when you're done, you have an overlapping /
and \
at the last step: X
就您而言,您知道如何绘制\
. 你可以很容易地适应这一点。但是使用我的方法,您可以/
在同一个screen
数组中绘制 a 。当你完成后,你有一个重叠/
,\
在最后一步:X
I used this method since we had to draw a circle and that's really a lot harder. And yes, nowadays I'd probably use std::vector<std::string> screen
but back then screens were really 80x25 :)
我使用这种方法是因为我们必须画一个圆圈,这真的很难。是的,现在我可能会使用,std::vector<std::string> screen
但当时屏幕真的是 80x25 :)
回答by TTT
#include "stdafx.h"
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
int i, d, a=1,b=12,c ,e=1;
for(i = 1; i <= 6; i++)
{
for (d=1; d<i;d++) {cout <<" ";}
cout<<"***";
for (c=a+1; c<b;c++) {cout <<" ";}
{cout<<"***";}
for(b=12-i;b<i;b++)
{cout<<"***";}
cout<<endl;
a++;
}
for( i = 1; i <= 3; i++)
{
for ( d=6; d>i;d--) {cout <<" ";}
cout<<"***";
for (c=0; c<e-1;c++) {cout <<" ";}
{cout<<"***";}
cout<<endl;
e++;
}
return 0;
}
回答by Ayman Mostafa
int n=11,i=0,k=0,j=0;
for(i=0;i<n;i++)
{
if(i<(n/2))
{
cout<<endl;
for(j=0;j<i;j++)
{
cout<<" ";
}
cout<<"*";
for(k=n/2;k>i;k--)
{
cout<<" ";
}
cout<<"*";
}
else
{
cout<<endl;
for(k=n-1;k>i;k--)
{
cout<<" ";
}
cout<<"*";
for(j=n/2;j<i;j++)
{
cout<<" ";
}
cout<<"*";
}
}
回答by Daniyal
#include<iostream>
using namespace std;
int main()
{
int i, j;
for(i = 1;i<= 5;i++)
{
for(j = 1;j<= 5;j++)
{
if((i == j)||(j==(5+1)-i))
{
cout << "*";
}
else{
cout << " ";
}
}
cout<< endl;
}
system("pause");
}
回答by chanod
#include "stdafx.h"
#include <iostream>
using namespace std;;
int _tmain(int argc, _TCHAR* argv[])
{
int i,k,j;
for (i=1;i<8;i++)
{
for (int k=0;k<i;k++)
{
cout<<" ";
}
cout<<"*";
for (int k=8;k>i;k--)
{
cout<<" ";
}
cout<<"*";
cout<<endl;
}
for (i=1;i<8;i++)
{
for (int k=8;k>i;k--)
{
cout<<" ";
}
cout<<"*";
for (int k=0;k<i;k++)
{
cout<<" ";
}
cout<<" *";
cout<<endl;
}
system("Pause");
return 0;
}