This article mainly introduced the C # Yang Hui triangle Implementation of relevant information, hope that through this article we can master this part of the content, the need for friends can refer to the next
Implementation of Yang Hui triangle in C #
Problem Description: Create a program to find triangles. The program prompts the user to enter data and then shows the law of the Yang Hui Triangle.
Input Description: Yang Hui triangle length, representing the value
Program output: Yang Hui triangle
using system;using system.collections.generic;using system.linq;using System.Text; Namespace consoleapplication2{class Program {static void Main (string[] args) {int length = 0;//Yang Hui triangle Console.Write ("Input yang hui triangle length:"); Length = Convert.ToInt32 (Console.ReadLine ());//Specifies the Yang Hui of the triangle int[][] A = new int[length][];//two-dimensional array for (int i = 0; i < A.length; i++) A[i] = new Int[i + 1];//traversal, assignment increment for (int j = 0; J < A.length; J + +) {a[j][0] = 1;//Put 1th column of the meta Vegetarian 1 A[j][j] = 1; Assign 1 for (int m = 1; m < a[j] to the rightmost element of each of the 1 columns. Length-1; m++) A[j][m] = A[j-1][m-1] + a[j-1][m];//The value of the remaining elements is calculated by the Yang Hui formula} for (int i = 0; i < a.length; i++)// Iterate through the array output Yang Hui triangle {for (int j = 0; J < A[i]. Length; J + +) Console.Write ("{0}\t", a[i][j]); Console.Write ("\ n"); } console.read (); } }}