杨辉
Blaise Pascal
这是某些程序员看完会哭的代码。
杨辉三角形(Yanghui Triangle),是一种序列数值的三角形几何排列,最早出现于南宋·数学家·杨辉1261年所著的《详解九章算法》一书。
欧洲学者,最先由帕斯卡(1623----1662)在1654年发现这一规律,故杨辉三角又称“帕斯卡三角形(Pascal Truangle)”。
图中暖色为偶数;冷色为奇数。
代码1:
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.DocumentText = Magic_Numbers.YangHui_Triangle(20);
}
private void button1_Click(object sender, EventArgs e)
{webBrowser1.DocumentText = Magic_Numbers.YangHui_Triangle(20);
}
代码2:
using System;
using System.Text;
namespace Legalsoft.Truffer.Algorithm
{
public static partial class Magic_Numbers
{
public static string YangHui_Triangle(int n)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("<style>");
sb.AppendLine(".n0 { float:left;width:90px;height:25px;line-height:25px;font-size:14px;text-align:center;border:dotted 1px #993333;background-color:#FFF9F9;margin-right:10px;margin-bottom:10px; } ");
sb.AppendLine(".n1 { float:left;width:90px;height:25px;line-height:25px;font-size:14px;text-align:center;border:dotted 1px #333399;background-color:#F9F9FF;margin-right:10px;margin-bottom:10px; } ");
sb.AppendLine("</style>");
sb.AppendLine("<center>");
long[] last = new long[n];
last[0] = 1;
for (int i = 1; i <= n; i++)
{
sb.AppendLine("<table><tr><td>");
long[] array = new long[i];
array[0] = 1;
sb.AppendLine("<div class='n"+ (array[0]%2) +"'>" + array[0] + "</div>");
for (int j = 1; j < i; j++)
{
array[j] = last[j - 1] + last[j];
sb.AppendLine("<div class='n" + (array[j] % 2) + "'>" + array[j] + "</div>");
}
for (int j = 0; j < i; j++)
{
last[j] = array[j];
}
sb.AppendLine("</td></tr></table>");
}
return sb.ToString();
}
}
}
using System;
using System.Text;namespace Legalsoft.Truffer.Algorithm
{public static partial class Magic_Numbers{public static string YangHui_Triangle(int n){StringBuilder sb = new StringBuilder();sb.AppendLine("<style>");sb.AppendLine(".n0 { float:left;width:90px;height:25px;line-height:25px;font-size:14px;text-align:center;border:dotted 1px #993333;background-color:#FFF9F9;margin-right:10px;margin-bottom:10px; } ");sb.AppendLine(".n1 { float:left;width:90px;height:25px;line-height:25px;font-size:14px;text-align:center;border:dotted 1px #333399;background-color:#F9F9FF;margin-right:10px;margin-bottom:10px; } ");sb.AppendLine("</style>");sb.AppendLine("<center>");long[] last = new long[n];last[0] = 1;for (int i = 1; i <= n; i++){sb.AppendLine("<table><tr><td>");long[] array = new long[i];array[0] = 1;sb.AppendLine("<div class='n"+ (array[0]%2) +"'>" + array[0] + "</div>");for (int j = 1; j < i; j++){array[j] = last[j - 1] + last[j];sb.AppendLine("<div class='n" + (array[j] % 2) + "'>" + array[j] + "</div>");}for (int j = 0; j < i; j++){last[j] = array[j];}sb.AppendLine("</td></tr></table>");}return sb.ToString();}}
}