杨辉三角形

来源:互联网 发布:centos 7.0 网卡配置 编辑:程序博客网 时间:2024/05/02 19:14

 // 软件技术2班
            //作者:B15

            //完成日期:2014年11月30日
            //描述问题:创建一个程序输出杨辉三角行
            //输出描述:输出杨辉三角形
            Console.ForegroundColor = ConsoleColor.Cyan;


            Console.WriteLine("-------------杨辉三角--------------");
            int[,] a = new int[11, 11];

            for (int i = 0; i < a.GetLength(0); i++)
            {
                a[i, 0] = 1;
                a[i, i] = 1;

                if (i > 1)
                {
                    for (int j = 1; j < i; j++)
                    {
                       a[i, j] = a[i - 1, j - 1] + a[i - 1, j];

                    }
                }
            }

            for (int i = 0; i < a.GetLength(0); i++)
            {
                for (int j = 0; j <= i; j++)
                {
                    Console.Write("{0,-4}", a[i, j]);
                }
                Console.WriteLine();
            }

            Console.ReadLine();

0 0