日期:2014-05-17  浏览次数:20850 次

求高手写一个程序!
一个有交叉数组 写起来的杨辉三角 应该怎么写! 高手在哪里

------解决方案--------------------
C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    static class Helper
    { 
        //m!/(n!*(m-n)!)
        public static int C(int m, int n)
        {
            return facEx(n + 1, m) / facEx(2, m - n);
        }

        private static int facEx(int from, int to)
        {
            int result = 1;
            for (int i = from; i <= to; i++)
            {
                result *= i;
            }
            return result;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            int n = 8;
            int[][] result = new int[n + 1][];
            for (int i = 0; i < n + 1; i++)
            {
                result[i] = new int[i + 1];
                for (int j = 0; j <= i; j++)
                {
                    result[i][j] = Helper.C(i, j);
                }
            }
            foreach (int[] item in result)
            {
                Console.WriteLine(string.Join(" ", item.Select(x => x.ToString().PadLeft(3, ' ')).ToArray()));
            }
        }
    }
}