算法示例四:( 邊計(jì)算邊輸出)
view plaincopy to clipboardprint? public static void GetYangHuiArray5(int n) { System.Diagnostics.Debug.Assert(n > 0); char space = '\t'; Console.WriteLine("n={0}", n); var l = n + 1; var r = l % 2; var h = (l + r) / 2; for (int i = 1; i <= h; i++) { StringBuilder line = new StringBuilder(); for (int j = 1; j <= i; j++) { line.Append(j); line.Append(space); } for (int j = l - 2 * i + r; j > 0; j--) { line.Append(i); line.Append(space); } for (int j = i - r; j > 0; j--) { line.Append(j); line.Append(space); } line.Remove(line.Length - 1, 1); Console.WriteLine(line.ToString()); } for (int i = h - r; i > 0; i--) { StringBuilder line = new StringBuilder(); for (int j = 1; j <= i; j++) { line.Append(j); line.Append(space); } for (int j = l - 2 * i + r; j > 0; j--) { line.Append(i); line.Append(space); } for (int j = i - r; j > 0; j--) { line.Append(j); line.Append(space); } line.Remove(line.Length - 1, 1); Console.WriteLine(line.ToString()); } }
算法示例五: (其他類似算法)
view plaincopy to clipboardprint? public static void GetYangHuiArray3(int n) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { GetMin(i, j); } for (int j = n - 1; j >= 1; j--) { GetMin(i, j); } Console.WriteLine(); } for (int i = n - 1; i >= 1; i--) { for (int j = 1; j <= n; j++) { GetMin(i, j); } for (int j = n - 1; j >= 1; j--) { GetMin(i, j); } Console.WriteLine(); } }
調(diào)用示例:
view plaincopy to clipboardprint? public static void Main(string[] args) { Console.WriteLine("input the rownum:"); int input = int.Parse(Console.ReadLine()); while (input > 0) { Console.WriteLine("n=" + input + ";"); GetYangHuiArray2(input); input = int.Parse(Console.ReadLine()); } }
效果:
拋磚,期望有更多的思路分享。
邀月注:本文版權(quán)由邀月和CSDN共同所有,轉(zhuǎn)載請注明出處。 助人等于自助! 3w@live.cn
轉(zhuǎn)載:http://blog.csdn.net/downmoon/archive/2010/07/05/5712679.aspx
本文鏈接:http://m.95time.cn/tech/program/2010/7784.asp
出處:邀月的博客周記
責(zé)任編輯:bluehearts
上一頁 一個類似于楊輝三角的數(shù)組算法思路 [2] 下一頁
◎進(jìn)入論壇網(wǎng)絡(luò)編程版塊參加討論
|