C#打印杨辉三角实例:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ArrayDemo1
{
class Program
{
static void Main(string[] args)
{
//打印杨辉三角
int i, j, k, m;
k = 7;
int[][] Y = new int[k][];//定义二维锯齿状数组
for(i = 0; i < Y.Length ; i++){
Y[i] = new int[i+1];
Y[i][0] = 1;
Y[i][i] = 1;
}
for(i = 2 ; i < Y.Length;i++)
{
for(j = 1 ; j < Y[i].Length-1;j++)
{
Y[i][j] = Y[i-1][j-1] + Y[i-1][j];
}
}
for (i = 0; i < Y.Length; i++)
{
for (j = 0; j < Y[i].Length; j++)
{
Console.Write("{0,5:d}", Y[i][j]);
}
Console.WriteLine();
}
Console.Read();
}
}
}