优点:只需要与杨辉三角形行数相同的存储空间。

缺点:因为数组长度不能在运行时确定,所以杨辉三角形的长度不能通过用户自行输入确定。

#include<iostream>

using namespace std;

const int N = 10;
int main()  
{  
      int a[N]={0};  
      a[0]=1;
      for(int i=1; i!=N;++i)  
        {
            int m = N - i;
           //输出空格
               while(m != 0 )
            {
                cout << " ";
                --m;
            }
            //输出
            for(int j=0;j != i;++j)  
               cout << a[j] << " ";  
            cout << endl;  
            //赋值
            a[i]=1; //最后一个元素赋值1
            for(int j=i-1;j!=0;--j)  
               a[j]=a[j]+a[j-1];      
        }  
       return 0;   
  }
[导入]一维数组输出杨辉三角形[导入]一维数组输出杨辉三角形
文章来源:http://liyuxia-life.spaces.live.com/Blog/cns!DA1B364675ACF35!270.entry

[导入]一维数组输出杨辉三角形

幸运草 2009-04-10 09:58 发表评论

相关文章:

  • 2021-06-30
  • 2022-12-23
  • 2021-06-20
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-29
猜你喜欢
  • 2021-08-08
  • 2022-03-05
  • 2021-12-19
  • 2022-12-23
  • 2021-05-21
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案