当前位置: 编程技术>.net/c#/asp.net
asp.net打印杨辉三角二种方法
来源: 互联网 发布时间:2014-08-30
本文导语: 在asp.net中用二种不同的方法实现杨辉三角,一种是用数组,最简单。 另一种是用牛顿二项式来解,输出杨辉三角。 例子: 代码示例: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace...
在asp.net中用二种不同的方法实现杨辉三角,一种是用数组,最简单。
另一种是用牛顿二项式来解,输出杨辉三角。
例子:
代码示例:
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
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
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
站内导航:
特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!