C#练习题答案: 帕斯卡三角【难度:2级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

mac2025-02-13  11

帕斯卡三角【难度:2级】:

答案1:

using System; using System.Collections.Generic; public static class Kata { public static List<int> PascalsTriangle(int n) { List<int> pt = new List<int>(); pt.Add(1); for(int i = 1; i < n; i++) { for(int j = 0; j <= i; j++) { if(j == 0 || j == i) pt.Add(1); else { pt.Add(pt[pt.Count - i] + pt[pt.Count - (i + 1)]); } } } return pt; } }

答案2:

using System; using System.Collections.Generic; public static class Kata { public static List<int> PascalsTriangle(int n) { List<int> result = new List<int>(); for (int i = 0; i < n; i++) { int element = 1; for (int j = 0; j <= i; j++) { result.Add(element); element = element * (i - j) / (j + 1); } } return result; } }

答案3:

using System; using System.Linq; using System.Collections.Generic; public static class Kata { public static List<int> PascalsTriangle(int n) { var depths = new List<List<int>> { new List<int> { 1 } }; for (int i = 1; i < n; i++) { var previousDepth = depths.Last(); var currentDepth = new List<int>(); for (int j = 0; j < i + 1; j++) { var n1 = (j - 1 < 0 ? 0 : previousDepth[j - 1]); var n2 = (j + 1 > previousDepth.Count ? 0 : previousDepth[j]); var number = n1 + n2; currentDepth.Add(number); } depths.Add(currentDepth); } return depths.SelectMany(d => d).ToList(); } }

答案4:

using System; using System.Collections.Generic; public static class Kata { public static List<int> PascalsTriangle(int n) { List<int> res = new List<int>() { 1 }; for (int i = 2; i < n + 1; i++) for (int j = 0; j < i; j++) res.Add(j == 0 || j == i - 1 ? 1 : res[res.Count - i] + res[res.Count - i + 1]); return res; } }

答案5:

using System; using System.Collections.Generic; using System.Linq; public static class Kata { public static List<int> PascalsTriangle(int n) { List<int> result = new List<int>(); List<List<int>> triangle = new List<List<int>>(); if (n >= 1) triangle.Add(new List<int> {1}); for (int i = 1; i < n; i++) { List<int> temp = new List<int>(); temp.Add(1); for (int j = 0; j < i-1; j++) { temp.Add(triangle[i-1][j] + triangle[i-1][j+1]); } temp.Add(1); triangle.Add(temp); } foreach (List<int> row in triangle) result.AddRange(row); return result; } }

答案6:

using System; using System.Collections.Generic; using System.Linq; public static class Kata { public static List<int> PascalsTriangle(int n) { if (n < 1) return null; var workingList = new List<int> {1}; var result = new List<int>(workingList); for (int i = 1; i < n; i++) { var addend = new List<int> {0}; addend.AddRange(workingList); workingList.Add(0); workingList = workingList.Zip(addend, (a, b) => a + b).ToList(); result.AddRange(workingList); } return result; } }

答案7:

using System; using System.Linq; using System.Collections.Generic; public static class Kata { public static List<int> PascalsTriangle(int n) { var result = new List<int>(); for (int row = 0; row < n; row++) { result.Add(1); for (int k = 1; k <= row; k++) { result.Add(result.Last() * (row + 1 - k) / k); } } return result; } }

答案8:

using System; using System.Collections.Generic; public static class Kata { public static List<int> PascalsTriangle(int n) { if (n < 1) { return new List<int>(); } var row = new Row(new[] {1}); int count = 1; while (count < n) { row.LastChild.AddChildRow(); count++; } return new List<int>(row.Numbers); } private class Row { private readonly List<int> values; private Row childRow; public Row(IEnumerable<int> values) { this.values = new List<int>(values); } public void AddChildRow() { var childValues = new List<int> {1}; for (int i = 0; i < this.values.Count - 1; i++) { childValues.Add(this.values[i] + this.values[i + 1]); } childValues.Add(1); this.childRow = new Row(childValues); } public IEnumerable<int> Numbers { get { foreach (var number in this.values) { yield return number; } if (this.childRow != null) { foreach (var number in this.childRow.Numbers) { yield return number; } } } } public Row LastChild { get { if (this.childRow != null) { return this.childRow.LastChild; } return this; } } } }

答案9:

using System; using System.Collections.Generic; using System.Linq; public static class Kata { public static List<int> PascalsTriangle(int n) { var l = new List<int>(); for (var i = 0; i < n; i++) { for (var j = 0; j <= i; j++) { if (j == 0 || j == i) { l.Add(1); continue; } var c = l.Count() - i; l.Add(l[c] + l[c - 1]); } } return l; } }

答案10:

using System; using System.Collections.Generic; public static class Kata { public static List<int> PascalsTriangle(int n) { List<int> a = new List<int>(); for (int x = 0; x<n; x++) { for (int i = 0; i<=x; i++) { a.Add(C(x, i)); } } return a; } static int C(int n,int k) { if (k>n/2) k=n-k; if (k==1) return n; if (k==0) return 1; return C(n-1,k)+C(n-1,k-1); } static int fac (int x) { return x<2? 1: x*fac(x-1); } }
最新回复(0)