C++进阶小程序(画菱形)

/*
编写一个程序,打印下列菱形形状。可以用输出语句打印单个星号(*)或单个空格。充分利用重复结构(用嵌套for结构),减少输出语句个数。
                *
             * * *
           * * * * *
        * * * * * * *
     * * * * * * * * *
       * * * * * * *
         * * * * *
           * * *
             *
*/

#include
using namespace std;

int main()
{
 int a;
 cout<<"Enter the length of Rhombus:";
 cin>>a;
 for(int i=1;i<=2*a-1&&i>0;i++)
 {
  if(i<=a)
  {
  for(int j=1;j<=a-i&&j>0;j++)
  {
   cout<<" ";
  }
  for(int k=1;k<=1+2*(i-1)&&k>0;k++)
  {
   cout<<"*";
  }
  cout<  }
  else
  {
     for(int j=1;j<=i-a&&j>0;j++)
   {
    cout<<" ";
   }
   for(int k=1;k<=(2*a-1)-2*(i-a)&&k>0;k++)
   {
    cout<<"*";
   }
   cout<  }
 }
 cin>>a;
 return 0;
}

你可能感兴趣的:(c++)