优点:只需要与杨辉三角形行数相同的存储空间。
缺点:因为数组长度不能在运行时确定,所以杨辉三角形的长度不能通过用户自行输入确定。
#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://c.services.spaces.live.com/CollectionWebService/c.gif?cid=982263437555584821&page=RSS%ef%bc%9a%e4%b8%80%e7%bb%b4%e6%95%b0%e7%bb%84%e8%be%93%e5%87%ba%e6%9d%a8%e8%be%89%e4%b8%89%e8%a7%92%e5%bd%a2&referrer=)
![](http://c.live.com/c.gif?NC=31263&NA=1149&PI=81873&RF=&DI=3919&PS=85545&TP=liyuxia-life.spaces.live.com>1=liyuxia-life)
文章来源:http://liyuxia-life.spaces.live.com/Blog/cns!DA1B364675ACF35!270.entry