using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
{
int x, y;
int[,] a = new int[20, 20];
for (x = 0; x <= 5; x++)
{
for (y = 0; y <= x; y++)
{
if (x == y || y == 0)
a[x, y] = 1;
else
a[x, y] = a[x - 1, y - 1] + a[x - 1, y];
Console.Write(" "+a[ x, y] );
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
}
首先,定义了一个20行20 列的矩形数组
其中,包含两个循环语句。
外层循环,表示在前六行内输出结果,你可以看下结果上显示的行数为六行,这里就可以对应了。
内层循环表示赋值过程。有两种赋值方式:
也就是除了1中两种情况之外的元素如何赋值的问题了.
举例:
a[2,1]=a[1,0]+a[1,1] = 1+1 =2 (可以检查下这个元素是否符合条件)
a[3,2] =a[2,1]+ a [3,1] = 2+1 =3
以此类推。
也就是等于其头顶两个元素之和。可以在纸上自己算算。
输出结果就是一个杨辉三角形,第一行为1,往下每一行第一个和最后一个为1,其余的第i个数为上一行第i-1和第i个数(也就是“肩上”的两个数)之和