【问题标题】:C++ For Loops, Asterisk Inverted Triangle [closed]C ++ For循环,星号倒三角形[关闭]
【发布时间】:2016-07-17 17:49:52
【问题描述】:

我需要你的帮助。 我需要编写一个程序,它有四个星号三角形,一个在另一个之上。我对第三个三角形有困难。一个有十行的倒三角形应该显示如下:

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

谁能帮助我?

   #include <iostream>

   using namespace std;

   int main()
   {
   cout << "\nAsterisk Triangluar Patterns:\n";
   cout << "\nPattern (a).\n\n";
   int i,j,rows;
   int ROWS = 10;
   //initiates the max limit of rows and/ columns the triangle may have 
   int k = 11;

    for(i=1;i<=rows;i++)
        {
            for(j=1;j<=i;j++)
                {
                    cout << "*";
                }
                cout << endl;
        }
    cout << "\nPattern (b).\n\n";
    for (i = 10; i >= 1; i = i-1)
    {
    // begin second loop from 1 to i
    for (j = 1; j <= i; j = j+1)
    {
        cout << "*";
    }
    cout << endl;
    }         

    cout << "\nPattern (c).\n\n";
   for (int i = 0; i < 10; i++)
   {
     for(int j = 0; j < i; j++)
    {
        cout <<"*";
    }
    cout << endl;
    }
    cout << "\nPattern (d).\n\n";
    for (i=1;i<=rows;i++)
        {
            for(j=1;j<=i;j++)
                {
                    cout << "*";
                }
                cout << endl;
        }


    cin.get();

    cin.get();

        return 0;
  }

【问题讨论】:

  • 好吧,你对如何做有什么想法(你已经提到了for循环)?您的教科书中还应该包含一些基本内容。
  • Xcode 与此无关,倒排索引也没有。如果你想窃取别人的代码,在这个网站上搜索“[cpp]倒三角形”会产生几十个的问题。但是,如果您不至少尝试自己首先进行操作,并且遇到问题,请发布您的代码在这里,也许我们可以提供帮助。
  • @Cyber​​_Lo 这四个三角形是什么?给他们看。
  • 这是我的代码_WhozCraig
  • @Cyber​​_Lo:查看您的 cmets,因为代码很难在评论中阅读。使用代码编辑您的帖子

标签: c++ algorithm loops char


【解决方案1】:

有很多方法可以完成这项任务。

例如

#include <iostream>
#include <iomanip>

int main()
{
    const size_t N = 10;

    for ( size_t i = 0; i < N; i++ )
    {
        std::cout << std::setw( i + 1 ) << std::setfill( ' ' ) << '*' 
                  << std::setw( N - i ) << std::setfill( '*' ) << '\n';
    }        
}    

程序输出是

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

或者通过标准算法

#include <iostream>
#include <algorithm>
#include <iterator>

int main()
{
    const size_t N = 10;

    for ( size_t i = 0; i < N; i++ )
    {
        auto out = std::ostream_iterator<char>( std::cout );
        *std::fill_n( std::fill_n( out, i, ' ' ), N - i, '*' ) = '\n';
    }            
}    

输出将与上图相同。

其实你需要写三个循环:一个外循环和两个内循环。

【讨论】:

    【解决方案2】:

    我能想到的最简单的方法:

    #include <iostream>
    #include <string>
    #include <iomanip>
    
    int main() {
    
       for ( int i = 10; i >= 0; --i )
           std::cout << std::setfill(' ') << std::setw(10) << std::string(i, '*') << std::endl;
           // or std::cout << std::string(10 - i, ' ') << std::string(i, '*') << std::endl;
    
       return 0;
    }
    

    但是,如果这让您感到困惑。你可以这样做:

    for ( int i = 10; i >= 0; i-- ) {
        for( int j = 0; j < 10 - i; j++ ) cout << " ";
        for( int j = 0; j < i; j++ ) cout << "*";
        cout << endl;
    }
    

    更接近你的代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多