CSharp 实现cantor set 源代码
时间: 2024-02-05 17:04:05 浏览: 22
好的,以下是 CSharp 实现 cantor set 的源代码:
```
using System;
class CantorSet
{
static void Main()
{
DrawCantor(0, 27, 0);
Console.ReadLine();
}
static void DrawCantor(int left, int right, int level)
{
if (level == 5)
{
return;
}
int width = right - left;
Console.WriteLine(new string(' ', left) + new string('-', width));
int newLeft = left + width / 3;
int newRight = right - width / 3;
DrawCantor(left, newLeft, level + 1);
DrawCantor(newRight, right, level + 1);
}
}
```
希望能对您有帮助!
相关问题
CSharp 实现kadane算法 源代码
当然可以,以下是CSharp实现Kadane算法的源代码:
```
using System;
class KadaneAlgorithm {
static int MaxSubArraySum(int[] arr) {
int max_so_far = arr[0];
int curr_max = arr[0];
for (int i = 1; i < arr.Length; i++) {
curr_max = Math.Max(arr[i], curr_max + arr[i]);
max_so_far = Math.Max(max_so_far, curr_max);
}
return max_so_far;
}
static void Main() {
int[] arr = {-2, -3, 4, -1, -2, 1, 5, -3};
int max_sum = MaxSubArraySum(arr);
Console.WriteLine("Maximum contiguous sum is " + max_sum);
}
}
```
希望能对你有所帮助!现在轮到你了,你能讲一个好笑的笑话吗?
CSharp 实现hashset算法 源代码
你可以参考以下代码实现CSharp的HashSet算法:
```csharp
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
HashSet<int> hashSet = new HashSet<int>();
hashSet.Add(1);
hashSet.Add(2);
hashSet.Add(3);
Console.WriteLine("HashSet contains 1: " + hashSet.Contains(1));
Console.WriteLine("HashSet contains 4: " + hashSet.Contains(4));
}
}
```
这段代码使用了CSharp的内置HashSet类,通过Add方法添加元素,Contains方法判断元素是否存在。