如何在c ++中使用for循环创建金字塔

时间:2013-09-12 09:10:44

标签: c++

大家好我想问一下如何使用c ++创建一个三角形?

其实我有我的代码,但我不知道如何将第一个星号置于三角形中心。我的三角形保持对齐。我怎样才能把它变成金字塔?

以下是我的代码。

#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;
}

8 个答案:

答案 0 :(得分:3)

对于三角形高度Y,则首先打印Y-1个空格,然后是星号和换行符。然后为下一行打印Y-2空格,接着是三个星号(比以前打印的多两个)和换行符。对于第三行打印Y-3空格,后跟五个星号(再次比前一行多两个)和换行符。继续,直到打印出整个三角形。

如下所示

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';
}

答案 1 :(得分:1)

要计算每行所需的空格数,请使用此算法:

numSpaces = (23 - x) / 2;

然后使用for循环来应用空格numSpaces次。

以下是完整的代码:

#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;
}

输出:

           *
          ***
         *****
        *******
       *********
      ***********
     *************
    ***************
   *****************
  *******************
 *********************
***********************

答案 2 :(得分:1)

一种方法 这是在一个外部循环中嵌套两个内部循环,一个循环打印空格,一个循环打印*(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;
}

输出:

    *
   ***
  *****
 *******
*********

答案 3 :(得分:0)

此代码位于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.
            }
        }
    }

答案 4 :(得分:0)

这是另一个不使用除法或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;
}

答案 5 :(得分:0)

仅使用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;
}

答案 6 :(得分:0)

我使用两个循环

来做到这一点

这是我的代码

#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;
}

,结果是

    *
   ***
  *****
 *******
*********

Url of code on Online compiler

你可以使用只有一个循环解决它,它简单易用

#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+="**";
    }
}

Url of code on Online compiler

答案 7 :(得分:0)

#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;
    }
}