利用数组和循环实现杨辉三角 (code)

       static void Main(string[] args)
        {
            int i, j;
            //int [] k;
            //初始化交错数组,并对行赋值。   
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
            int [][] arr = new int[10][];

            for (i = 0; i < 10; i++ )
            {
                //定义交错数组有多少列.(因为一行的列数等于这个行数+1)

                //k = new int[i + 1];(这样做的结果会出现未将对象引用到实例: 未将k数组引入到arr数组的列上)
                arr[i] = new int[i+1];

                for (j = 0; j < arr[i].Length; j++)
                {
                    //对数组赋值
                    if(j==0 || j==i)
                    {
                        //每行中的首列和末列赋值
                        arr[i][j] = 1;
                    }
                    else
                    {
                        //每行中的(首列+1)到(末列-1)赋值
                        arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][ j ];
                    }
                    Console.Write(arr[i][j] + " ");
                }
                Console.WriteLine();
            }
        }

posted @ 2007-10-11 19:13  jun.ma  阅读(224)  评论(0编辑  收藏  举报