liyuxia713

蹒跚前行者

常用链接

统计

Algorithms

C++

最新评论

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

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

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

#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

posted on 2009-04-10 09:58 幸运草 阅读(1189) 评论(0)  编辑 收藏 引用 所属分类: Algorithms


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理