用c#输出杨辉三角

问题与代码

// Copyright (c) 2014软件技术2班        
    // All rights rwserved.        
    // 作者:B10        
    // 完成日期:2014年11月27日        
    // 版本号:v1.0        
    //  
    //编写程序:输出n=10的杨辉三角(共11行)
    class Program
    {
        static void Main(string[] args)
        {
            Console.Title = "杨辉三角";
            int[,] yhsj = new int[11, 11];

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

                if (x > 1)
                {
                    for (int y = 1; y < yhsj.GetLength(1); y++)
                    {
                        yhsj[x, y] = yhsj[x - 1, y - 1] + yhsj[x - 1, y];

                    }
                }
            }
            for (int x = 0; x < yhsj.GetLength(0); x++)
            {
                for (int y = 0; y <= x; y++)
                {
                    Console.Write("{0,-4}", yhsj[x, y]);
                }
                Console.WriteLine();
            }

            Console.Read();
            
        }
    }

输出

《用c#输出杨辉三角》

总结

当初老师布置这个作业时我心里一抽,想着数学的?这种三角关系我不会, 不过仔细想想,也和数学没啥关系,心里阴影啊

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