打印菱形
1、打印实心菱形
#include<iostream>
using namespace std;
int main ( )
{
cout < < "Please enter the side length of the diamond:" ;
int n;
cin > > n;
for ( int i = 0 ; i< n; i++ )
{
for ( int j = 0 ; j< ( n - 1 - i) ; j++ )
{
cout < < " " ;
}
for ( int j = 0 ; j< 2 * i + 1 ; j++ )
{
cout < < "*" ;
}
cout < < endl;
}
for ( int i = 0 ; i< n - 1 ; i++ )
{
for ( int j = 0 ; j< i + 1 ; j++ )
{
cout < < " " ;
}
for ( int j = 0 ; j< 2 * ( n - 1 ) - ( 1 + 2 * i) ; j++ )
{
cout < < "*" ;
}
cout < < endl;
}
system ( "pause" ) ;
return 0 ;
}
2、打印空心菱形
#include<iostream>
using namespace std;
int main ( )
{
cout < < "Please enter the side length of the diamond:" ;
int n;
cin > > n;
for ( int i = 0 ; i< n; i++ )
{
for ( int j = 0 ; j< ( n - 1 - i) ; j++ )
{
cout < < " " ;
}
for ( int j = 0 ; j < 2 * i + 1 ; j++ )
{
if ( j == 0 || j == 2 * i)
cout < < "*" ;
else
cout < < " " ;
}
cout < < endl;
}
for ( int i = 0 ; i < n - 1 ; i++ )
{
for ( int j = 0 ; j < i + 1 ; j++ )
{
cout < < " " ;
}
for ( int j = 0 ; j < 2 * ( n - 1 ) - ( 1 + 2 * i) ; j++ )
{
if ( j == 0 || j == 2 * ( n - i - 2 ) )
cout < < "*" ;
else
cout < < " " ;
}
cout < < endl;
}
system ( "pause" ) ;
return 0 ;
}