zoukankan      html  css  js  c++  java
  • [导入]一维数组输出杨辉三角形

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

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

    #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 发表评论
  • 相关阅读:
    使用Mybatis时报错Invalid bound statement (not found):
    MyBatis学习(3)
    选数字
    看程序写结果
    NP
    完全平方数
    hahaha
    haha

    凝视
  • 原文地址:https://www.cnblogs.com/liyuxia713/p/2540786.html
Copyright © 2011-2022 走看看