注册 登录
编程论坛 C++教室

杨辉三角形

nardoloveme 发布于 2008-11-13 16:18, 1141 次点击
输出以下的杨辉三角形(要求输出10行)
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
. . .  .  . .
. . .  .  . .
. . .  .  . .
给多点提示,不太会用算法表达出来。
5 回复
#2
ronaldowsy2008-11-13 17:00
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    const int n=10;
    int a[n][n],i,j;
    for (i=1;i<n;i++)
    {
        a[i][1]=1;
        a[i][i]=1;
    }
    for (i=3;i<n;i++)
    {
        for (j=2;j<=i-1;j++)
        {
            a[i][j]=a[i-1][j-1]+a[i-1][j];
        }
    }
    for (i=1;i<n;i++)
    {
        for (j=1;j<=i;j++)
        {
            cout<<setw(6)<<a[i][j]<<" ";
        }
        cout<<endl;
    }
    return 0;
}
#3
sundalei2008-11-13 19:00
#include<iostream>
using namespace std;

int main()
{
    int yangHui[10][10];
    int row,column;
    for(row=0;row<10;row++)
    {
        for(column=0;column<=row;column++)
        {
            if(column==0||column==row)
                yangHui[row][column]=1;
            else
                yangHui[row][column]=yangHui[row-1][column-1]+yangHui[row-1][column];
        }
    }
    cout<<" 杨辉三角形的前十行是:"<<endl;
    for(row=0;row<10;row++)
    {
        for(column=0;column<=row;column++)
        {
            cout<<" "<<yangHui[row][column];
        }
        cout<<endl;
    }
    return 0;
}
#4
sunkaidong2008-11-13 19:40
都不错...其实数学模型做好,就很好了..加油啊...呵呵
#5
biaoxiaoqun2009-11-09 17:25
有没有用到队列的那种
#6
honghong882011-11-10 21:37

#include<iostream>
using namespace std;


int main()
{int a[10][10],i=0 ,j=0 ;
for(i=0;i<10;i++)
{a[i][0]=1;
cout<<a[i][0]<<" ";
for(j=1;j<=i;j++)
{if(j==i)
a[i][j]=1;
if(j<i)
 a[i][j]=a[i-1][j-1]+a[i-1][j];

cout<<""<<a[i][j]<<" ";

}

cout<<endl;

}



}

1