C# 中楊輝三角的實(shí)現(xiàn)
問(wèn)題描述:創(chuàng)建一個(gè)程序來(lái)求三角形。該程序提示用戶輸入數(shù)據(jù),然后顯示出楊輝三角的規(guī)律。            
// 輸入描述:楊輝三角長(zhǎng),代表數(shù)值             
// 程序輸出:楊輝三角  
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication2{  class Program  {    static void Main(string[] args)    {      int length = 0;//楊輝三角形的長(zhǎng)度       Console.Write("輸入楊輝三角長(zhǎng)度:");      length = Convert.ToInt32(Console.ReadLine());//指定楊輝三角形的長(zhǎng)度      int[][] a = new int[length][];//二維數(shù)組      for (int i = 0; i < a.Length; i++)        a[i] = new int[i + 1];//遍歷,賦值增量      for (int j = 0; j < a.Length; j++)      {        a[j][0] = 1; //把第1列的元素都賦1        a[j][j] = 1; //把每1列最右邊的元素都賦1        for (int m = 1; m < a[j].Length - 1; m++)          a[j][m] = a[j - 1][m - 1] + a[j - 1][m];//其余元素的值由楊輝公式計(jì)算      }      for (int i = 0; i < a.Length; i++) //遍歷數(shù)組輸出楊輝三角形      {        for (int j = 0; j < a[i].Length; j++)          Console.Write("{0}/t", a[i][j]);        Console.Write("/n");      }      Console.Read();    }  }}如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選