杨辉三角型打印

注:

利用杨辉三角形可以快速的列出牛顿二项式分解。
如:杨辉三角第6行值是 1 6 15 20 15 6 1
对应的二项式分解就是
( m + n ) 6 = m 6 + 6 m 5 n + 15 m 4 n 2 + 20 m 3 n 3 + 15 m 2 n 4 + 6 m n 5 + n 6 (m+n)^6=m^6+6m^5n+15m^4n^2+20m^3n^3+15m^2n^4+6mn^5+n^6 (m+n)6=m6+6m5n+15m4n2+20m3n3+15m2n4+6mn5+n6

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 杨辉三角
{
	public class Program
	{
		private const string spaceChar = "      ";
		/// 
		/// 主函数
		/// 
		/// 
		public static void Main( string[] args )
		{
			PrintYangHui( 13 );

			//PrintYangHui( 8, 8 );

			YangHui( 13 );

			Console.ReadLine();
		}
		/// 
		/// 递归调用-打印三角形
		/// 
		/// 
		/// 
		public static int[] PrintYangHui( int n )
		{
			if ( n == 1 ) return Print( new int[] { 1 } );
			if ( n == 2 )
			{
				PrintYangHui( n - 1 );
				return Print( new int[] { 1, 1 } );
			}
			int[] pR = PrintYangHui( n - 1 );
			int[] R = new int[n];
			R[0] = 1;
			for ( int i = 1; i < R.Length - 1; i++ ) R[i] = pR[i] + pR[i - 1];
			R[R.Length - 1] = 1;
			return Print( R );
		}
		/// 
		/// 格式化输出到控制台的样式
		/// 
		/// 
		public static int[] Print( int[] result )
		{
			StringBuilder Space = new StringBuilder();
			string spaceChar = "      ";
			for ( int i = 0; i < result.Length; i++ ) Console.Write( result[i].ToString() + spaceChar );
			Console.WriteLine();
			return result;
		}
		/// 
		/// 打印居中的杨辉三角形
		/// 
		/// 总的层次数,用来控制第一行的前面需要多少个分隔符
		/// 打印第几层
		/// 
		public static int[] PrintYangHui( int totalLevel, int level )
		{
			if ( Level == 1 ) return Print( totalLevel, new int[] { 1 } );
			if ( Level == 2 )
			{
				PrintYangHui( totalLevel, Level - 1 );
				return Print( totalLevel, new int[] { 1, 1 } );
			}
			int[] pR = PrintYangHui( totalLevel, Level - 1 );
			int[] R = new int[Level];
			R[0] = 1;
			for ( int i = 1; i < R.Length - 1; i++ ) R[i] = pR[i] + pR[i - 1];
			R[R.Length - 1] = 1;
			return Print( totalLevel, R );
		}
		/// 
		/// 打印居中的杨辉三角形
		/// 
		/// 
		public static int[] Print( int totalLevel, int[] result )
		{
			StringBuilder Space = new StringBuilder();
			for ( int i = totalLevel; i > result.Length; i-- ) Space.Append( spaceChar );
			Console.Write( Space );
			for ( int i = 0; i < result.Length; i++ ) Console.Write( FormatString( 5, result[i] ) + spaceChar );
			Console.WriteLine();
			return result;
		}
		/// 
		/// 格式化数字串
		/// 
		/// 
		/// 
		/// 
		public static string FormatString( int Len, int num )
		{
			char[] outString;
			string strNum = num.ToString();
			int startIndex = 0;
			if ( strNum.Length < Len )
			{
				outString = new char[Len];
				startIndex = ( Len - strNum.Length ) / 2;
			}
			else
				outString = new char[strNum.Length];

			for ( int i = 0; i < outString.Length; i++ )
			{
				if ( i >= startIndex && i < startIndex + strNum.Length )
					outString[i] = strNum[i - startIndex];
				else
					outString[i] = ' ';
			}

			return new String( outString );
		}
		/// 
		/// 打印杨辉三角型的某一层,通过位置,计算出每层每个位置的数值,节省运行空间
		/// 
		/// 本次打印二项式N次方的系数
		public static void Print( int N )
		{
			for ( int k = 0; k <= N; k++ )
			{
				int V = 1; // 用来保存N阶乘除以i阶乘的值
				int S = 1; // 用来保存N-i的阶乘
				int I = 0;
				for ( int i = k + 1; i <= N; i++ )	// N阶除以(N-i)阶后再除以i阶
				{
					V *= i;
					I = ( N - i + 1 );
					S *= I;
					if ( V % I == 0 )// 如果此时V可以被I整除,就先整除,以免最终V的阶乘太大导致数据溢出
					{
						V /= I;
						S /= I;
					}
					else if ( V % S == 0 )// 如果此时V可以被S整除,就先整除,以免最终V的阶乘数太大导致数据溢出
					{
						V /= S;
						S = 1;
					}
				}
				
				V /= S;

				Console.Write( V.ToString() + spaceChar );
			}
			Console.WriteLine();
		}
		/// 
		/// 打印居中的杨辉三角形
		/// 
		/// 
		/// 
		public static void YangHui( int totalLevel)
		{
			for ( int i = 0; i < totalLevel; i++ )
			{
				//StringBuilder Space = new StringBuilder();
				//for ( int j = TotalLevel; j > i; j-- ) Space.Append( spaceChar );
				//Console.Write( Space );
				Print( i );
			}
		}
	}
}

你可能感兴趣的:(java,开发语言)