面试题-一个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;
  }(王朝网络 wangchao.net.cn)
原文地址:https://www.cnblogs.com/baiduligang/p/4247259.html