用一个for循环输出以下棱形
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
用一个for循环输出以下棱形
int func( int n)
//输出菱形星形
{
char ch='*';
char str=' ';
int N=2*n-1;
int MAX=N*N-1;
int m;
--n;
for(int i=0;i<=MAX;i++)
{
m=n-i/N;
if(m<0)m=-m;
if(i%N==N-1)
{
if(i/N==n)cout<<ch;
cout<<endl;
}
else
{
if((i%N)>=m&&(i%N)<=(2*n-m)&&(i%N%2!=i/N%2)) cout<<ch;
else cout<<str;
}
}
return 0;
}
博客主要围绕用一个for循环输出菱形展开,给出了具体的菱形图案示例,并展示了实现该功能的代码。代码中定义了函数func,通过一系列计算和条件判断,在一个for循环内完成菱形的输出。

被折叠的 条评论
为什么被折叠?



