using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 杨辉三角
{
public class Program
{
private const string spaceChar = " ";
/// <summary>
/// 主函数
/// </summary>
/// <param name="args"></param>
public static void Main( string[] args )
{
PrintYangHui( 13 );
//PrintYangHui( 8, 8 );
YangHui( 13 );
Console.ReadLine();
}
/// <summary>
/// 打印三角形
/// </summary>
/// <param name="n"></param>
/// <returns></returns>
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 );
}
/// <summary>
/// 打印出杨辉三角形
/// </summary>
/// <param name="result"></param>
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;
}
/// <summary>
/// 打印
居中的杨辉三角形
/// </summary>
/// <param name="n"></param>
/// <returns></returns>
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 );
}
/// <summary>
/// 打印居中的杨辉三角形
/// </summary>
/// <param name="result"></param>
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;
}
/// <summary>
/// 格式化数字串
/// </summary>
/// <param name="Len"></param>
/// <param name="num"></param>
/// <returns></returns>
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 );
}
/// <summary>
/// 打印杨辉三角型的某一层
/// </summary>
/// <param name="N">本次打印二项式N次方的系数</param>
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();
}
/// <summary>
/// 打印居中的杨辉三角形
/// </summary>
/// <param name="n"></param>
/// <returns></returns>
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 );
}
}
}
}