首页 > 代码库 > 杨辉三角

杨辉三角

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {

            int[,] number = new int[11, 11];

            for (int i = 0; i < number.GetLength(0); i++)
            {

                    for (int j = 0; j < number.GetLength(1); j++)
                    {

                        if (i == j || j == 0) number[i, j] = 1;
                        else if (i != 0) number[i, j] = number[i - 1, j - 1] + number[i - 1, j];
                        else number[i, j] = 0;

                }
            }

            for (int i = 0; i < number.GetLength(0); i++)
            {
                for (int j = 0; j < number.GetLength(1); j++)
                {
                    
                 Console.Write("{0} ", number[i, j]);

                }
                 Console.WriteLine();
            }
            Console.ReadKey();
        }
    }
}

杨辉三角