C#打印杨辉三角实例

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();
        }
    }
}

 

    原文作者:杨辉三角问题
    原文地址: https://blog.csdn.net/ma451152002/article/details/9219949
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞