- N的阶乘,实现算法
int n) { if (n == 1) { return 1; } else { return n * N(n - 1); } }
-
2个已经排好序的数组,现按大小顺序组合成一个新数组,实现算法
int[] b) { int[] newArray = new int[a.Length + b.Length]; int count = a.Length + b.Length; int aIndex = 0; int bIndex = 0; for (int i = 0; i < count; i++) { if (aIndex >= i) { newArray[i] = a[aIndex]; continue; } if (bIndex >= i) { newArray[i] = b[bIndex]; continue; } if (a[aIndex] <= b[bIndex]) { newArray[i] = a[aIndex]; aIndex++; } else { newArray[i] = b[bIndex]; bIndex++; } Console.WriteLine(newArray[i]); } }
- 写出1,2,2,3,4,5的所有组合,但是4不能在第3位,3和5不能相邻。
string>(); public static void OutputConposition() { char[] arr = { '1', '2', '2', '3', '4', '5' }; string str = new string(arr); Function(arr, 0, 5); } public static void Function(char[] c, int begin, int end) { if (begin == end) { string str = new string(c); if (str[2] == '4') { return; } if (str.Contains("35") || str.Contains("53")) { return; } listStr.Add(str); return; } for (int i = begin; i <= end; i++) { Swap(c, begin, i); Function(c, i + 1, end); Swap(c, begin, i); } } public static void Swap(char[] arr, int a, int b) { char temp; temp = arr[a]; arr[a] = arr[b]; arr[b] = temp; }
相关文章: