杨辉三角(C#)

using System; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { method2(); } private void method1() { Console.WriteLine(“input the row number:”); int rowno = int.Parse(Console.ReadLine()); //int [][] a = new int [rowno+1][rowno+1]; int[,] a = new int[rowno + 1, rowno + 1]; for (int i = 0; i < rowno; i++) { for (int j = 0; j <= i; j++) { if (j == 0 || j == i) { a[i, j] = 1; } else { a[i, j] = a[i – 1, j – 1] + a[i – 1, j]; } Console.Write(“{0}/t”, a[i, j]); } Console.WriteLine(); } Console.ReadKey(); } private static void method2() { Console.WriteLine(“input the row number:”); int rowno = int.Parse(Console.ReadLine()); int c = 0; for (int i = 0; i < rowno; i++) { for (int j = 0; j < rowno – i; j++) { Console.Write(” “); } c = 1; for (int j = 0; j <= i; j++) { Console.Write(“{0} “, c); c = c * (i – j) / (j + 1); } Console.WriteLine(); } Console.ReadKey(); } } }

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