C++ 如何在c ++中使用for循环创建金字塔
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18760080/
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
how to create a pyramid using for loop in c++
提问by Jerielle
Hello guys I just want to ask how can I create a triangle using c++?
大家好,我想问一下如何使用c ++创建三角形?
Actually I have my code but I don't have an idea how to center the first asterisk in the triangle. My triangle is left align. How can I make it a pyramid?
实际上我有我的代码,但我不知道如何将三角形中的第一个星号居中。我的三角形左对齐。我怎样才能使它成为金字塔?
Here's my code below.
下面是我的代码。
#include<iostream>
using namespace std;
int main(){
int x,y;
char star = '*';
char space = ' p ';
int temp;
for(x=1; x <= 23; x++){
if((x%2) != 0){
for(y=1; y <= x ; y++){
cout << star;
}
cout << endl;
}
}
return 0;
}
回答by Some programmer dude
For a triangle och height Y, then first print Y-1 spaces, followed by an asterisk and a newline. Then for the next line print Y-2 spaces, followed by three asterisks (two more than previously printed) and a newline. For the third line print Y-3 spaces followed by five asterisks (again two more than previous line) and a newline. Continue until you have printed your whole triangle.
对于一个高度为 Y 的三角形,首先打印 Y-1 个空格,然后是一个星号和一个换行符。然后在下一行打印 Y-2 个空格,然后是三个星号(比之前打印的多两个)和一个换行符。对于第三行,打印 Y-3 空格,后跟五个星号(比前一行多两个)和一个换行符。继续直到你打印了整个三角形。
Something like the following
类似于以下内容
int asterisks = 1;
for (int y = HEIGHT; y > 0; --y, asterisks += 2)
{
for (int s = y - 1; s >= 0; --s)
std::cout << ' ';
for (int a = 0; a < asterisks; ++a)
std::cout << '*';
std::cout << '\n';
}
回答by jmstoker
To calculate the number of spaces needed to center each row use this algorithm:
要计算每行居中所需的空间数,请使用以下算法:
numSpaces = (23 - x) / 2;
numSpaces = (23 - x) / 2;
and then a for
loop to apply the spaces numSpaces
times.
然后是一个for
循环来应用空格numSpaces
时间。
Here is the complete code:
这是完整的代码:
#include<iostream>
using namespace std;
int main(){
int x,y;
char star = '*';
char space = ' p ';
int temp;
int numSpaces = 0;
for(x=1; x <= 23; x++){
if((x%2) != 0){
numSpaces = (23 - x) / 2; // Calculate number of spaces to add
for(int i = 0; i < numSpaces; i++) // Apply the spaces
{
cout << " ";
}
for(y=1; y <= x ; y++){
cout << star;
}
cout << endl;
}
}
return 0;
}
And the output:
和输出:
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*********************
***********************
回答by RKk
One way to do this is to nest two inner loops, one to print spaces and one to print *(s), inside an outer loop that steps down the screen from line to line.
一种方法是嵌套两个内部循环,一个用于打印空格,另一个用于打印 *(s),在从一行到另一行逐行向下屏幕的外循环中。
#include <iostream>
using namespace std;
int main(){
int row = 5;
for(int i=0; i<row; i++){
for(int j=row; j>i; j--){
cout << " ";
}
for(int k=0; k<2*i+1; k++){
cout << "*";
}
cout << endl;
}
return 0;
}
Output:
输出:
*
***
*****
*******
*********
回答by Akshay
This code is in C#, but you can convert it in c++.
此代码使用 C#,但您可以将其转换为 C++。
class Program
{
static void Main()
{
int n = 5; // Number of lines to print.
for(int i = 1; i<= n; i++){
//loop for print space in the order (4,3,2,1,0) i.e n-i;
for(int j= 1; j<= n-i; j++){
Console.Write(" ");
}
//loop for print * in the order (1,3,5,7,9..) i.e 2i-1;
for(int k= 1; k<= 2*i-1; k++){
Console.Write("*");
}
Console.WriteLine(); // Next Line.
}
}
}
回答by Michael Swartz
Here's another solution that doesn't use division or if
statements
这是另一个不使用除法或if
语句的解决方案
#include <iostream.h>
int main() {
int height = 17, rowLength, i, j, k;
char symbol = '^';
// print a pyramid with a default height of 17
rowLength = 1;
for (i = height; i > 0; i--) { // print a newline
cout << endl;
for (j = 1; j <= i; j++) // print leading spaces
cout << " ";
for (k = 0; k < rowLength; k++) // print the symbol
cout << symbol;
rowLength = rowLength + 2; // for each row increase the number of symbols to print
}
cout << "\n\n ";
return 0;
}
回答by Ankush Malik
Star pyramid using for loop only:-
仅使用 for 循环的星形金字塔:-
#include <iostream>
#include <conio.h>
#include <iomanip>
using namespace std;
int main()
{
int n;
cout << "enter the number of rows of pyramid you want : ";
cin >> n;
"\n";
for (int i = 0; i <= n; ++i) {
cout << "\n";
for (int j = 0; j <= n - i; ++j) {
cout << " ";
}
for (int k = 1; k <= i; k++) {
cout << setw(3) << "*";
}
}
return 0;
}
回答by Basheer AL-MOMANI
I did that using two loops
我使用两个循环来做到这一点
here is my code
这是我的代码
#include <iostream>
#include <string>
using namespace std;
int main() {
int rows, star, spaces;
int number_of_stars = 5;
int number_of_rows = number_of_stars;
string str1 = "*";
for (rows=1; rows <= number_of_rows; rows++) {
for (spaces=1; spaces <= number_of_stars; spaces++) {
if (spaces==number_of_stars)
{
cout<<str1;
str1+="**";
}
else
cout<<(" ");
}
cout<<("\n");
number_of_stars = number_of_stars - 1;
}
return 0;
}
and the result is
结果是
*
***
*****
*******
*********
Url of code on Online compiler
and you can solve it using only one loop, its simple and easy
你可以只用一个循环来解决它,它简单易行
#include <iostream>
#include <string>
using namespace std;
int main()
{
int numberOfLines=4;
string spaces=string( numberOfLines , ' ' );//this is 4 spaces
string stars="*";
while(spaces!="")
{
cout<<spaces<<stars<<endl;
spaces = spaces.substr(0, spaces.size()-1);
stars+="**";
}
}
回答by waleed wali khan
#include<iostream>
using namespace std;
int for1(int &row);//function declaration
int rows;//global variable
int main()
{
cout<<"enter the total number of rows : ";
cin>>rows;
for1(rows);//function calling
cout<<"just apply a space at the end of the asteric and volla ";
}
int for1(int &row)//function definition
{
for(int x=1;x<=row;x++)//for loop for the lines
{
for(int y=row;y>=x;y--) //for loop for spaces (dynamic loop)
{
cout<<" ";
}
for(int k=1;k<=x*2-x;k++)//for loop for asteric
{
cout<<"* ";/*apply a space and you can turn a reverse right angle triangle into a pyramid */
}
cout<<endl;
}
}