C # Example of implementing the Yang Hui Triangle

Source: Internet
Author: User
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 (); }  }}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.