c#控制台杨辉三角形

来源:互联网 发布:c语言求100以内的素数 编辑:程序博客网 时间:2024/04/28 21:25
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication44{    class Program    {        static void Main(string[] args)        {            int i, j, k, m;            k = 11;            int[][] a = new int[k][];            for (i = 0; i < a.Length; i++)            {                a[i] = new int[i + 1];                a[i][0] = 1;                a[i][i] = 1;            }            for (i = 2; i <a.Length; i++)            {                for (j = 1; j < a[i].Length - 1; j++)                {                    a[i][j] = a[i - 1][j - 1] + a[i - 1][j];                }            }            for (i = 0; i < a.Length; i++)            {                for (j = 0; j < a[i].Length; j++)                {                    Console.Write("{0,5:d}", a[i][j]);                }                Console.WriteLine();            }            Console.Read();          }    }}
这里用到二位数组和for循环   当用到for循环时  要清楚使用的条件<img src="http://img.blog.csdn.net/20141125012534539?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbHN3bG92ZXRzdw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />


0 0
原创粉丝点击