杨辉三角

来源:互联网 发布:汽车网络推广 编辑:程序博客网 时间:2024/05/16 19:11
  1. //  
  2. // Copyright (c) 2014软件技术1班  
  3. // All rights reserved.   
  4. // 作    者:A30  黄勇华  
  5. // 完成日期:2014年 11 月 24 日   
  6. // 版 本 号:v1.0   
  7. //   
  8. // 问题描述:用数组在控制台输出杨辉三角
  9. // 输入描述:   
  10. // 程序输出:输出杨辉三角形
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Threading.Tasks;


  16. namespace 杨辉三角
  17. {
  18.     class Program
  19.     {
  20.         static void Main(string[] args)
  21.         {




  22.             int length = 10;  
  23.             int[][] a = new int[length][];
  24.             for (int i = 0; i < a.Length; i++)
  25.                 a[i] = new int[i + 1];
  26.             for (int j = 0; j < a.Length; j++)
  27.             {
  28.                 a[j][0] = 1;
  29.                 a[j][j] = 1;
  30.                 for (int m = 1; m < a[j].Length - 1; m++)
  31.                     a[j][m] = a[j - 1][m - 1] + a[j - 1][m];
  32.             }
  33.             for (int i = 0; i < a.Length; i++)
  34.             {
  35.                 for (int j = 0; j < a[i].Length; j++)
  36.                     Console.Write("{0}\t", a[i][j]);
  37.                 Console.Write("\n");
  38.             }
  39.             Console.Read();
  40.  
  41.            
  42.         }
  43.     }
  44. }
  45.  
  46.  
  47.  
  48. 输出:
  49.  
  50.  
  51.  总结:
  52. 1.学会对数组的应用
  53. 2.加深对数组输出的理解
  54.  
  55.  
  56.  
  57.  

0 0
原创粉丝点击