diff --git a/test1-original-code/1.1/1.1.csproj b/test1-original-code/1.1/1.1.csproj new file mode 100644 index 0000000000000000000000000000000000000000..bd12445c5cff10da46fe8a3a7d234b57afb12321 --- /dev/null +++ b/test1-original-code/1.1/1.1.csproj @@ -0,0 +1,11 @@ + + + + Exe + net8.0 + _1._1 + enable + enable + + + diff --git a/test1-original-code/1.1/program.cs b/test1-original-code/1.1/program.cs new file mode 100644 index 0000000000000000000000000000000000000000..299486c62e0c0329ecc80f4f82409e87d98935c5 --- /dev/null +++ b/test1-original-code/1.1/program.cs @@ -0,0 +1,24 @@ +using System;//引入命名空间 +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace _1.cs//定义命名空间 +{ + class Program//定义类 + { + static void Main(string[] args)//主方法,程序入口 + { + int n1 = 10; + int n2 = 20; + int transnum;//用来转换的变量 + + transnum = n1; + n1 = n2; + n2 = transnum; + + Console.WriteLine($"n1 = {n1}, n2 = {n2}"); + } + } +} diff --git a/test1-original-code/1.10/1.10.csproj b/test1-original-code/1.10/1.10.csproj new file mode 100644 index 0000000000000000000000000000000000000000..e1f967aefd87690e5074b76a352381fa1cdf5a11 --- /dev/null +++ b/test1-original-code/1.10/1.10.csproj @@ -0,0 +1,11 @@ + + + + Exe + net8.0 + _1._10 + enable + enable + + + diff --git a/test1-original-code/1.10/Program.cs b/test1-original-code/1.10/Program.cs new file mode 100644 index 0000000000000000000000000000000000000000..aa55ff5ee22d6be3837702694ee2c19c425aa058 --- /dev/null +++ b/test1-original-code/1.10/Program.cs @@ -0,0 +1,13 @@ +using System; +class Program +{ + static void Main() + { + string input = " Hello World ! 你好 世界 ! "; + + // 先去掉首尾空格 -> 按空格分割(去掉多余空格)-> 用单个空格拼接 + string result = string.Join(" ", input.Trim().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)); + + Console.WriteLine($"处理后字符串: \"{result}\""); + } +} diff --git a/test1-original-code/1.11/1.11.csproj b/test1-original-code/1.11/1.11.csproj new file mode 100644 index 0000000000000000000000000000000000000000..c3b78523658ab8e864b7587d1bdccffef73c6ce4 --- /dev/null +++ b/test1-original-code/1.11/1.11.csproj @@ -0,0 +1,11 @@ + + + + Exe + net8.0 + _1._11 + enable + enable + + + diff --git a/test1-original-code/1.11/Program.cs b/test1-original-code/1.11/Program.cs new file mode 100644 index 0000000000000000000000000000000000000000..7b5b3153a8cf6cdd80b8cea6ee4fd25d8538a0aa --- /dev/null +++ b/test1-original-code/1.11/Program.cs @@ -0,0 +1,122 @@ +using System; + +namespace RockPaperScissors +{ + // 枚举定义游戏选项 + public enum Choice + { + Scissors = 1, // 剪刀 + Rock = 2, // 石头 + Paper = 3 // 布 + } + + // 玩家类 + class Player + { + public string Name { get; set; } + + public Player(string name) + { + Name = name; + } + + // 玩家选择出拳 + public Choice MakeChoice() + { + while (true) + { + Console.Write("请选择:1. 剪刀 2. 石头 3. 布 :"); + string input = Console.ReadLine(); + + if (int.TryParse(input, out int choice) && choice >= 1 && choice <= 3) + { + return (Choice)choice; + } + Console.WriteLine("输入无效,请输入 1-3 之间的数字!"); + } + } + } + + // 电脑类 + class Computer + { + private static readonly Random random = new Random(); + + // 电脑随机出拳 + public Choice MakeChoice() + { + return (Choice)random.Next(1, 4); + } + } + + // 游戏逻辑类 + class Game + { + private Player player; + private Computer computer; + + public Game(string playerName) + { + player = new Player(playerName); + computer = new Computer(); + } + + // 判断胜负 + private string DetermineWinner(Choice playerChoice, Choice computerChoice) + { + if (playerChoice == computerChoice) + return "平局!"; + + if ((playerChoice == Choice.Scissors && computerChoice == Choice.Paper) || + (playerChoice == Choice.Rock && computerChoice == Choice.Scissors) || + (playerChoice == Choice.Paper && computerChoice == Choice.Rock)) + { + return $"{player.Name} 获胜!"; + } + + return "电脑获胜! "; + } + + // 游戏开始 + public void Start() + { + Console.WriteLine("\n欢迎来到猜拳小游戏!\n"); + + while (true) + { + // 玩家出拳 + Choice playerChoice = player.MakeChoice(); + Console.WriteLine($"{player.Name} 选择了:{playerChoice}"); + + // 电脑出拳 + Choice computerChoice = computer.MakeChoice(); + Console.WriteLine($"电脑选择了:{computerChoice}"); + + // 计算结果 + Console.WriteLine(DetermineWinner(playerChoice, computerChoice)); + + // 询问是否继续 + Console.Write("\n是否继续游戏?(Y/N): "); + string continueGame = Console.ReadLine().Trim().ToUpper(); + + if (continueGame != "Y") + { + Console.WriteLine("游戏结束,谢谢参与!"); + break; + } + } + } + } + + // 主程序 + class Program + { + static void Main() + { + Console.Write("请输入您的名字: "); + string playerName = Console.ReadLine(); + + Game game = new Game(playerName); + game.Start(); + } + } \ No newline at end of file diff --git a/test1-original-code/1.2/1.2.csproj b/test1-original-code/1.2/1.2.csproj new file mode 100644 index 0000000000000000000000000000000000000000..89a52e495ab2e332856ceafa789ee4ad93d7ff31 --- /dev/null +++ b/test1-original-code/1.2/1.2.csproj @@ -0,0 +1,11 @@ + + + + Exe + net8.0 + _1._2 + enable + enable + + + diff --git a/test1-original-code/1.2/Program.cs b/test1-original-code/1.2/Program.cs new file mode 100644 index 0000000000000000000000000000000000000000..1e1ca8f24bdf86a4012e347636cde68c3e643331 --- /dev/null +++ b/test1-original-code/1.2/Program.cs @@ -0,0 +1,36 @@ +using System;//引入命名空间 +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace _1.cs//定义命名空间 +{ + class Program//定义类 + { + static void Swap(ref int n1, ref int n2) + {/*静态方法与实例方法的区别 + 实例方法:属于类的实例(对象),必须通过类的实例来调用。 + 静态方法:属于类本身,而不是类的实例,可以直接通过类名调用。 + Main方法是静态的,而Swap方法是实例方法。 + 静态方法(如Main)不能直接调用实例方法(如Swap), + 因为实例方法依赖于类的实例(对象),而静态方法不依赖于任何实例。*/ + n1 = 10; + n2 = 20; + int exchange_num = 0; + exchange_num = n1; + n1 = n2; + n2 = exchange_num; + } + + + static void Main(string[] args)//主方法,程序入口 + { + int n1 = 0; + int n2 = 0;//使用ref时必须要先赋值 + Swap(ref n1, ref n2); // 调用方法 + Console.WriteLine($"n1: {n1}, n2: {n2}"); // 输出 + } + } +} + diff --git a/test1-original-code/1.3/1.3.csproj b/test1-original-code/1.3/1.3.csproj new file mode 100644 index 0000000000000000000000000000000000000000..660fa23a36a542d9b8550c3095e14cd8c1bd920e --- /dev/null +++ b/test1-original-code/1.3/1.3.csproj @@ -0,0 +1,11 @@ + + + + Exe + net8.0 + _1._3 + enable + enable + + + diff --git a/test1-original-code/1.3/Program.cs b/test1-original-code/1.3/Program.cs new file mode 100644 index 0000000000000000000000000000000000000000..f847c3a4bece38729db58b9e225a56235d63e1ca --- /dev/null +++ b/test1-original-code/1.3/Program.cs @@ -0,0 +1,35 @@ +using System;//引入命名空间 +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace _1.cs//定义命名空间 +{ + class Program//定义类 + { + static void Swap(out int n1, out int n2) + {/*静态方法与实例方法的区别 + 实例方法:属于类的实例(对象),必须通过类的实例来调用。 + 静态方法:属于类本身,而不是类的实例,可以直接通过类名调用。 + Main方法是静态的,而Swap方法是实例方法。 + 静态方法(如Main)不能直接调用实例方法(如Swap), + 因为实例方法依赖于类的实例(对象),而静态方法不依赖于任何实例。*/ + n1 = 10; + n2 = 20; + int exchange_num = 0; + exchange_num = n1; + n1 = n2; + n2 = exchange_num; + } + + + static void Main(string[] args)//主方法,程序入口 + { + int n1; + int n2; + Swap(out n1, out n2); // 调用方法 + Console.WriteLine($"n1: {n1}, n2: {n2}"); // 输出 + } + } +} \ No newline at end of file diff --git a/test1-original-code/1.4/1.4.csproj b/test1-original-code/1.4/1.4.csproj new file mode 100644 index 0000000000000000000000000000000000000000..1b6f4c019ffc930eb3d9fd1948c045c4ca871139 --- /dev/null +++ b/test1-original-code/1.4/1.4.csproj @@ -0,0 +1,11 @@ + + + + Exe + net8.0 + _1._4 + enable + enable + + + diff --git a/test1-original-code/1.4/Program.cs b/test1-original-code/1.4/Program.cs new file mode 100644 index 0000000000000000000000000000000000000000..a952824af34509e493a89b045af7ab447eecbcbf --- /dev/null +++ b/test1-original-code/1.4/Program.cs @@ -0,0 +1,22 @@ +using System; +class Program +{ + static int MaxValue(params int[] numbers)//最大值函数 + { + int max = numbers[0]; + foreach (int num in numbers) + { + if (num > max) + { + max = num; + } + } + return max; + } + static void Main() + { + Console.WriteLine(MaxValue(3, 5, 11, 27, 15, 1)); + Console.WriteLine(MaxValue(10, 40, 1000)); + Console.WriteLine(MaxValue(5, 3, 4, 2, 0, 1, 9)); + } +} diff --git a/test1-original-code/1.5/1.5.csproj b/test1-original-code/1.5/1.5.csproj new file mode 100644 index 0000000000000000000000000000000000000000..1a0783178cf81d713f85cae918bdf046098fd83f --- /dev/null +++ b/test1-original-code/1.5/1.5.csproj @@ -0,0 +1,11 @@ + + + + Exe + net8.0 + _1._5 + enable + enable + + + diff --git a/test1-original-code/1.5/Program.cs b/test1-original-code/1.5/Program.cs new file mode 100644 index 0000000000000000000000000000000000000000..85c0d859292743f53ec3eece386a98e80caf9202 --- /dev/null +++ b/test1-original-code/1.5/Program.cs @@ -0,0 +1,24 @@ +using System; +class Program +{ + static int FibonacciSum(int n)//斐波那契数列 + { + int a = 0, b = 1, sum = 0; + + for (int i = 1; i <= n; i++) + { + sum += a; + int trans = a + b; + a = b; + b = trans;//用来保存前两个数的数值便于求下一个数 + } + + return sum; + } + static void Main() + { + int n = 20; + long sum = FibonacciSum(n); + Console.WriteLine($"斐波那契数列前 {n} 项的和为: {sum}"); + } +} diff --git a/test1-original-code/1.6/1.6.csproj b/test1-original-code/1.6/1.6.csproj new file mode 100644 index 0000000000000000000000000000000000000000..4c1bf4bde7c618ae6192bb2ac979674fa00e113e --- /dev/null +++ b/test1-original-code/1.6/1.6.csproj @@ -0,0 +1,11 @@ + + + + Exe + net8.0 + _1._6 + enable + enable + + + diff --git a/test1-original-code/1.6/Program.cs b/test1-original-code/1.6/Program.cs new file mode 100644 index 0000000000000000000000000000000000000000..0d1f328a206d8eef9db690c3407eacfaa5023021 --- /dev/null +++ b/test1-original-code/1.6/Program.cs @@ -0,0 +1,32 @@ +using System; +class Program +{ + static void Main() + { + int rows = 10; // 需要打印的行数 + int[][] triangle = new int[rows][]; // 定义二维数组 + + // 计算杨辉三角形 + for (int i = 0; i < rows; i++) + { + triangle[i] = new int[i + 1]; // 初始化当前行的数组 + triangle[i][0] = triangle[i][i] = 1; // 每行的第一个和最后一个元素是1 + + for (int j = 1; j < i; j++) + { + triangle[i][j] = triangle[i - 1][j - 1] + triangle[i - 1][j]; // 中间元素 = 上一行相邻两个数之和 + } + } + + // 打印杨辉三角形 + for (int i = 0; i < rows; i++) + { + Console.Write(new string(' ', (rows - i) * 2)); // 控制对齐(空格) + for (int j = 0; j < triangle[i].Length; j++) + { + Console.Write(triangle[i][j] + " "); // 打印当前元素 + } + Console.WriteLine(); // 换行 + } + } +} \ No newline at end of file diff --git a/test1-original-code/1.7/1.7.csproj b/test1-original-code/1.7/1.7.csproj new file mode 100644 index 0000000000000000000000000000000000000000..5781c455f392f588e3053072afc6c517b8c8b295 --- /dev/null +++ b/test1-original-code/1.7/1.7.csproj @@ -0,0 +1,11 @@ + + + + Exe + net8.0 + _1._7 + enable + enable + + + diff --git a/test1-original-code/1.7/Program.cs b/test1-original-code/1.7/Program.cs new file mode 100644 index 0000000000000000000000000000000000000000..654c048eb42f27afe8bdcd73dc4c5fef766c5376 --- /dev/null +++ b/test1-original-code/1.7/Program.cs @@ -0,0 +1,55 @@ +using System; +class Program +{ + // 冒泡排序算法 + static void BubbleSort(int[] arr) + { + int n = arr.Length; + bool swapped; // 标记是否发生交换 + + for (int i = 0; i < n - 1; i++) + { + swapped = false; + + for (int j = 0; j < n - 1 - i; j++) + { + if (arr[j] > arr[j + 1]) // 相邻元素比较 + { + Swap(ref arr[j], ref arr[j + 1]); + swapped = true; // 发生交换 + } + } + + // 如果本轮没有发生交换,则说明数组已经有序,提前结束 + if (!swapped) break; + } + } + // 交换两个元素 + static void Swap(ref int a, ref int b) + { + int temp = a; + a = b; + b = temp; + } + // 打印数组 + static void PrintArray(int[] arr) + { + foreach (var item in arr) + { + Console.Write(item + " "); + } + Console.WriteLine(); + } + static void Main() + { + int[] arr = { 20, 16, 78, 61, 12, 99, 57, 34, 8 }; + + Console.WriteLine("排序前数组:"); + PrintArray(arr); + + BubbleSort(arr); // 调用冒泡排序 + + Console.WriteLine("\n排序后数组:"); + PrintArray(arr); + } +} diff --git a/test1-original-code/1.8/1.8.csproj b/test1-original-code/1.8/1.8.csproj new file mode 100644 index 0000000000000000000000000000000000000000..d139dfb445dfff2a75f77fe895372823c0d31162 --- /dev/null +++ b/test1-original-code/1.8/1.8.csproj @@ -0,0 +1,11 @@ + + + + Exe + net8.0 + _1._8 + enable + enable + + + diff --git a/test1-original-code/1.8/Program.cs b/test1-original-code/1.8/Program.cs new file mode 100644 index 0000000000000000000000000000000000000000..7f457f8b9b348a1a0430ef369f8ab2c8beeae83b --- /dev/null +++ b/test1-original-code/1.8/Program.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; + +class Program +{ + static void Main() + { + string text = "患者:“大夫,我咳嗽得很重。”大夫:“你多大年记?”患者:“七十五岁。”大夫:“二十岁咳嗽吗”患者:“不咳嗽。” 大夫:“四十岁时咳嗽吗?” 患者:“也不咳嗽。” 大夫:“那现在不咳嗽,还要等到什么时咳嗽?”"; + + string target = "咳嗽"; + List indices = FindOccurrences(text, target); + + Console.WriteLine($"“{target}” 出现次数: {indices.Count}"); + Console.WriteLine("出现索引位置:"); + foreach (var index in indices) + { + Console.WriteLine(index); + } + } + + // 查找字符串 target 在 text 中的所有索引 + static List FindOccurrences(string text, string target) + { + List indices = new List(); + int index = text.IndexOf(target); // 第一次出现的位置 + + while (index != -1) + { + indices.Add(index); + index = text.IndexOf(target, index + target.Length); // 从上次索引位置之后继续查找 + } + + return indices; + } +} + diff --git a/test1-original-code/1.9/1.9.csproj b/test1-original-code/1.9/1.9.csproj new file mode 100644 index 0000000000000000000000000000000000000000..0a0a091153e32287629fe106d2f0461f309b2d99 --- /dev/null +++ b/test1-original-code/1.9/1.9.csproj @@ -0,0 +1,11 @@ + + + + Exe + net8.0 + _1._9 + enable + enable + + + diff --git a/test1-original-code/1.9/Program.cs b/test1-original-code/1.9/Program.cs new file mode 100644 index 0000000000000000000000000000000000000000..a12dd4358bd95a81d311802a540356a224d956d0 --- /dev/null +++ b/test1-original-code/1.9/Program.cs @@ -0,0 +1,22 @@ +using System; +class Program +{ + static void Main() + { + string dateStr = "2020年10月1日"; + + // 使用 '年'、'月'、'日' 分割字符串 + string[] parts = dateStr.Split(new char[] { '年', '月', '日' }, StringSplitOptions.RemoveEmptyEntries); + + if (parts.Length == 3) + { + Console.WriteLine($"年: {parts[0]}"); + Console.WriteLine($"月: {parts[1]}"); + Console.WriteLine($"日: {parts[2]}"); + } + else + { + Console.WriteLine("日期格式不正确!"); + } + } +} diff --git a/test1-zy/1.cs b/test1-zy/1.cs new file mode 100644 index 0000000000000000000000000000000000000000..bb6925ae406943191084a2d4b72f9e0aeca81c04 --- /dev/null +++ b/test1-zy/1.cs @@ -0,0 +1,17 @@ +using System; + +class Program +{ + static void Main(string[] args) + { + int n1 = 10, n2 = 20; + + // 交换 + n1 = n1 + n2; // n1 = 30 + n2 = n1 - n2; // n2 = 10 + n1 = n1 - n2; // n1 = 20 + + Console.WriteLine("n1 = " + n1); + Console.WriteLine("n2 = " + n2); + } +} \ No newline at end of file diff --git a/test1-zy/10.cs b/test1-zy/10.cs new file mode 100644 index 0000000000000000000000000000000000000000..eb1df371bf93d632c5876fc6eb44f7fdb097d0d9 --- /dev/null +++ b/test1-zy/10.cs @@ -0,0 +1,33 @@ +using System; +using System.Text; + +class Program +{ + static void Main(string[] args) + { + string text = " Hello World ! 你好 世界 ! "; + // 去掉两端空格 + text = text.Trim(); + // 替换连续空格为单个空格 + StringBuilder sb = new StringBuilder(); + bool isSpace = false; + + foreach (char c in text) + { + if (c == ' ') + { + if (!isSpace) + { + sb.Append(c); + isSpace = true; + } + } + else + { + sb.Append(c); + isSpace = false; + } + } + Console.WriteLine($"处理后的字符串: \"{sb.ToString()}\""); + } +} \ No newline at end of file diff --git a/test1-zy/2.cs b/test1-zy/2.cs new file mode 100644 index 0000000000000000000000000000000000000000..1e3f143029350e6b8b3e39058cbec45e7182a170 --- /dev/null +++ b/test1-zy/2.cs @@ -0,0 +1,26 @@ +using System; + +class Program +{ + static void Main(string[] args) + { + int n1 = 10, n2 = 20; + + Console.WriteLine("交换前:"); + Console.WriteLine("n1 = " + n1); // n1 = 10 + Console.WriteLine("n2 = " + n2); // n2 = 20 + + // 调用交换 + Swap(ref n1, ref n2); + + Console.WriteLine("\n交换后:"); + Console.WriteLine("n1 = " + n1); // n1 = 20 + Console.WriteLine("n2 = " + n2); // n2 = 10 + } + static void Swap(ref int a, ref int b) + { + int temp = a; + a = b; + b = temp; + } +} \ No newline at end of file diff --git a/test1-zy/3.cs b/test1-zy/3.cs new file mode 100644 index 0000000000000000000000000000000000000000..5463da68e29c7e7c41dbc166ac1ce012dc2ee68f --- /dev/null +++ b/test1-zy/3.cs @@ -0,0 +1,41 @@ +using System; + +class Program +{ + static void Main(string[] args) + { + int num1 = 20; + int num2 = 5; + + //计算结果 + int addResult, subResult, mulResult; + double divResult; + + Calculate(num1, num2, out addResult, out subResult, out mulResult, out divResult); + + Console.WriteLine($"加法结果: {addResult}"); + Console.WriteLine($"减法结果: {subResult}"); + Console.WriteLine($"乘法结果: {mulResult}"); + Console.WriteLine($"除法结果: {divResult}"); + } + + static void Calculate(int a, int b, out int add, out int sub, out int mul, out double div) + { + // 加 + add = a + b; + // 减 + sub = a - b; + // 乘 + mul = a * b; + // 除 + if (b != 0) + { + div = (double)a / b; //确保结果是浮点数 + } + else + { + div = 0; //除数为 0,返回 0 + Console.WriteLine("警告:除数不能为 0!"); + } + } +} \ No newline at end of file diff --git a/test1-zy/4.cs b/test1-zy/4.cs new file mode 100644 index 0000000000000000000000000000000000000000..fd07eb7c7669b0f6c7aa7ac4f5d541cf7e855b6f --- /dev/null +++ b/test1-zy/4.cs @@ -0,0 +1,37 @@ +using System; + +class Program +{ + static void Main(string[] args) + { + int max1 = FindMax(10, 20, 30, 5, 15); // 多参数 + int max2 = FindMax(100); // 单参数 + int max3 = FindMax(); + + Console.WriteLine("最大值1: " + max1); + Console.WriteLine("最大值2: " + max2); + Console.WriteLine("最大值3: " + max3); + } + + // 计算最大值 + static int FindMax(params int[] numbers) + { + if (numbers == null || numbers.Length == 0) + { + Console.WriteLine("警告:未提供任何参数,返回默认值 0。"); + return 0; + } + + int max = numbers[0]; // 假设第一个数为最大值 + + foreach (int num in numbers) + { + if (num > max) + { + max = num; + } + } + + return max; + } +} \ No newline at end of file diff --git a/test1-zy/5.cs b/test1-zy/5.cs new file mode 100644 index 0000000000000000000000000000000000000000..7980981bf83b1e9f022275aa17b592fc9385372b --- /dev/null +++ b/test1-zy/5.cs @@ -0,0 +1,26 @@ +using System; + +class Program +{ + static void Main(string[] args) + { + int n = 20; + int sum = FibonacciSum(n); + + Console.WriteLine($"斐波那契数列前 {n} 项的和为: {sum}"); + } + + static int FibonacciSum(int n) + { + int a = 0, b = 1, sum = 0; + + for (int i = 0; i < n; i++) + { + sum += a; + int temp = a; + a = b; + b = temp + b; + } + return sum; + } +} \ No newline at end of file diff --git a/test1-zy/6.cs b/test1-zy/6.cs new file mode 100644 index 0000000000000000000000000000000000000000..85270ad9df7fd9aa8b80f331c6e4c11e409d0c23 --- /dev/null +++ b/test1-zy/6.cs @@ -0,0 +1,30 @@ +using System; + +class Program +{ + static void Main(string[] args) + { + int rows = 10; + PrintPascalTriangle(rows); + } + + static void PrintPascalTriangle(int rows) + { + for (int i = 0; i < rows; i++) + { + int number = 1; + for (int j = 0; j < rows - i; j++) + { + Console.Write(" "); // 打印空格对齐 + } + + for (int j = 0; j <= i; j++) + { + Console.Write($"{number,4}"); + number = number * (i - j) / (j + 1); + } + + Console.WriteLine(); + } + } +} \ No newline at end of file diff --git a/test1-zy/7.cs b/test1-zy/7.cs new file mode 100644 index 0000000000000000000000000000000000000000..c42aa3ad76d50fabe9eee29f2d405d366dd449b9 --- /dev/null +++ b/test1-zy/7.cs @@ -0,0 +1,35 @@ +using System; + +class Program +{ + static void Main(string[] args) + { + int[] arr = { 20, 16, 78, 61, 12, 99, 57, 34, 8 }; + + BubbleSort(arr); + + Console.WriteLine("排序后的数组:"); + foreach (int num in arr) + { + Console.Write(num + " "); + } + } + + static void BubbleSort(int[] arr) + { + int n = arr.Length; + for (int i = 0; i < n - 1; i++) + { + for (int j = 0; j < n - 1 - i; j++) + { + if (arr[j] > arr[j + 1]) + { + // 交换 + int temp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + } + } + } + } +} \ No newline at end of file diff --git a/test1-zy/8.cs b/test1-zy/8.cs new file mode 100644 index 0000000000000000000000000000000000000000..b39c94fb5be23104bdb25d8563232cad15e5866f --- /dev/null +++ b/test1-zy/8.cs @@ -0,0 +1,24 @@ +using System; + +class Program +{ + static void Main(string[] args) + { + string text = "患者:“大夫,我咳嗽得很重。” 大夫:“你多大年记?” 患者:“七十五岁。” 大夫:“二十岁咳嗽吗”患者:“不咳嗽。” 大夫:“四十岁时咳嗽吗?” 患者:“也不咳嗽。” 大夫:“那现在不咳嗽,还要等到什么时咳嗽?”"; + + string keyword = "咳嗽"; + int count = 0; + + Console.WriteLine($"“{keyword}”出现的索引位置:"); + int index = text.IndexOf(keyword); + + while (index != -1) + { + count++; + Console.WriteLine(index); + index = text.IndexOf(keyword, index + keyword.Length); + } + + Console.WriteLine($"“{keyword}”总共出现了 {count} 次。"); + } +} \ No newline at end of file diff --git a/test1-zy/9.cs b/test1-zy/9.cs new file mode 100644 index 0000000000000000000000000000000000000000..4437f852ae1571654e6ab53431edd6c3c7ad6f9a --- /dev/null +++ b/test1-zy/9.cs @@ -0,0 +1,17 @@ +using System; + +class Program +{ + static void Main(string[] args) + { + string dateStr = "2020年10月1日"; + + int year = int.Parse(dateStr.Substring(0, 4)); + int month = int.Parse(dateStr.Substring(5, 2)); + int day = int.Parse(dateStr.Substring(8, 1)); + + Console.WriteLine($"年: {year}"); + Console.WriteLine($"月: {month}"); + Console.WriteLine($"日: {day}"); + } +} \ No newline at end of file diff --git a/test1-zy/test1.sln b/test1-zy/test1.sln new file mode 100644 index 0000000000000000000000000000000000000000..257813b300918a0a895552dda5785bf6c016faa0 --- /dev/null +++ b/test1-zy/test1.sln @@ -0,0 +1,79 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.10.34916.146 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "1", "1\1.csproj", "{46117978-FA30-4FE0-983B-F79BAE62DF77}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "2", "2\2.csproj", "{F0076FF9-F759-47E9-A35B-B23A0C05AF81}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "3", "3\3.csproj", "{8AE7EA22-7EDB-4701-B42D-E1E3B4A64226}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "4", "4\4.csproj", "{C03979F5-6F9B-4366-9024-7AC06B58065C}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "5", "5\5.csproj", "{E0AFE13D-DBC6-4B7C-B201-DF2D5B48ED82}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "6", "6\6.csproj", "{BDE6B3FD-C3FA-4066-AADF-801ABA9A7F85}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "7", "7\7.csproj", "{54A789D8-6F8A-492F-B647-E07C9531ECBF}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "8", "8\8.csproj", "{8EECFF69-700D-4FA3-A1FF-10CE7A8669B2}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "9", "9\9.csproj", "{6520D456-B28A-4CCA-859A-8183F3F190FE}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "10", "10\10.csproj", "{F5079637-EF6D-44CA-8DF8-C454A7DE082A}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {46117978-FA30-4FE0-983B-F79BAE62DF77}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {46117978-FA30-4FE0-983B-F79BAE62DF77}.Debug|Any CPU.Build.0 = Debug|Any CPU + {46117978-FA30-4FE0-983B-F79BAE62DF77}.Release|Any CPU.ActiveCfg = Release|Any CPU + {46117978-FA30-4FE0-983B-F79BAE62DF77}.Release|Any CPU.Build.0 = Release|Any CPU + {F0076FF9-F759-47E9-A35B-B23A0C05AF81}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F0076FF9-F759-47E9-A35B-B23A0C05AF81}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F0076FF9-F759-47E9-A35B-B23A0C05AF81}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F0076FF9-F759-47E9-A35B-B23A0C05AF81}.Release|Any CPU.Build.0 = Release|Any CPU + {8AE7EA22-7EDB-4701-B42D-E1E3B4A64226}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8AE7EA22-7EDB-4701-B42D-E1E3B4A64226}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8AE7EA22-7EDB-4701-B42D-E1E3B4A64226}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8AE7EA22-7EDB-4701-B42D-E1E3B4A64226}.Release|Any CPU.Build.0 = Release|Any CPU + {C03979F5-6F9B-4366-9024-7AC06B58065C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C03979F5-6F9B-4366-9024-7AC06B58065C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C03979F5-6F9B-4366-9024-7AC06B58065C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C03979F5-6F9B-4366-9024-7AC06B58065C}.Release|Any CPU.Build.0 = Release|Any CPU + {E0AFE13D-DBC6-4B7C-B201-DF2D5B48ED82}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E0AFE13D-DBC6-4B7C-B201-DF2D5B48ED82}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E0AFE13D-DBC6-4B7C-B201-DF2D5B48ED82}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E0AFE13D-DBC6-4B7C-B201-DF2D5B48ED82}.Release|Any CPU.Build.0 = Release|Any CPU + {BDE6B3FD-C3FA-4066-AADF-801ABA9A7F85}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BDE6B3FD-C3FA-4066-AADF-801ABA9A7F85}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BDE6B3FD-C3FA-4066-AADF-801ABA9A7F85}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BDE6B3FD-C3FA-4066-AADF-801ABA9A7F85}.Release|Any CPU.Build.0 = Release|Any CPU + {54A789D8-6F8A-492F-B647-E07C9531ECBF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {54A789D8-6F8A-492F-B647-E07C9531ECBF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {54A789D8-6F8A-492F-B647-E07C9531ECBF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {54A789D8-6F8A-492F-B647-E07C9531ECBF}.Release|Any CPU.Build.0 = Release|Any CPU + {8EECFF69-700D-4FA3-A1FF-10CE7A8669B2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8EECFF69-700D-4FA3-A1FF-10CE7A8669B2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8EECFF69-700D-4FA3-A1FF-10CE7A8669B2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8EECFF69-700D-4FA3-A1FF-10CE7A8669B2}.Release|Any CPU.Build.0 = Release|Any CPU + {6520D456-B28A-4CCA-859A-8183F3F190FE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6520D456-B28A-4CCA-859A-8183F3F190FE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6520D456-B28A-4CCA-859A-8183F3F190FE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6520D456-B28A-4CCA-859A-8183F3F190FE}.Release|Any CPU.Build.0 = Release|Any CPU + {F5079637-EF6D-44CA-8DF8-C454A7DE082A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F5079637-EF6D-44CA-8DF8-C454A7DE082A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F5079637-EF6D-44CA-8DF8-C454A7DE082A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F5079637-EF6D-44CA-8DF8-C454A7DE082A}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {29793C4F-B847-40DF-A602-52BA67C3EBC9} + EndGlobalSection +EndGlobal diff --git "a/\345\256\236\351\252\2141-\345\255\231\351\207\221\347\216\211/.keep" "b/\345\256\236\351\252\2141-\345\255\231\351\207\221\347\216\211/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\345\256\236\351\252\214\344\270\200/1.cs" "b/\345\256\236\351\252\214\344\270\200/1.cs" new file mode 100644 index 0000000000000000000000000000000000000000..3aacd48f3d1a735b883451587076eb5c668518f9 --- /dev/null +++ "b/\345\256\236\351\252\214\344\270\200/1.cs" @@ -0,0 +1,16 @@ +/* +using System; +class Program +{ + static void Main() + { + int n1 = 10, n2 = 20; + int n3 = 0; + n3 = n1; + n1 = n2; + n2 = n3; + Console.WriteLine($"n1为{n1},n2为{n2}"); + Console.ReadLine(); + } +} +*/ diff --git "a/\345\256\236\351\252\214\344\270\200/10.cs" "b/\345\256\236\351\252\214\344\270\200/10.cs" new file mode 100644 index 0000000000000000000000000000000000000000..93ac92dd2e7a2ec5688bc197a071310cd6d47ae9 --- /dev/null +++ "b/\345\256\236\351\252\214\344\270\200/10.cs" @@ -0,0 +1,38 @@ +using System; +using System.Text; +class Program +{ + static void Main() + { + string input = " Hello World ! 你好 世界 ! "; + // 去掉两端空格 + string trimmed = input.Trim(); + // 将连续空格替换为一个空格 + string result = ReplaceMultipleSpaces(trimmed); + Console.WriteLine($"原始字符串: \"{input}\""); + Console.WriteLine($"处理后的字符串: \"{result}\""); + Console.ReadLine(); + } + static string ReplaceMultipleSpaces(string input) + { + StringBuilder sb = new StringBuilder(); + bool isSpace = false; + foreach (char c in input) + { + if (c == ' ') + { + if (!isSpace) + { + sb.Append(c); + isSpace = true; + } + } + else + { + sb.Append(c); + isSpace = false; + } + } + return sb.ToString(); + } +} \ No newline at end of file diff --git "a/\345\256\236\351\252\214\344\270\200/2.cs" "b/\345\256\236\351\252\214\344\270\200/2.cs" new file mode 100644 index 0000000000000000000000000000000000000000..2a22d0f7d254c296fe8fd64b483c4275b3604134 --- /dev/null +++ "b/\345\256\236\351\252\214\344\270\200/2.cs" @@ -0,0 +1,19 @@ +/* +using System; +class Program +{ + static void Main() + { + int n1 = 10, n2 = 20; + Swap(ref n1, ref n2); + Console.WriteLine($"n1为{n1},n2为{n2}"); + Console.ReadLine(); + } + static void Swap(ref int a, ref int b) + { + int temp = a; + a = b; + b = temp; + } +} +*/ \ No newline at end of file diff --git "a/\345\256\236\351\252\214\344\270\200/3.cs" "b/\345\256\236\351\252\214\344\270\200/3.cs" new file mode 100644 index 0000000000000000000000000000000000000000..b9b55f74abf9666a0683da41b467eecee60898fa --- /dev/null +++ "b/\345\256\236\351\252\214\344\270\200/3.cs" @@ -0,0 +1,33 @@ +/* +using System; +class Program +{ + static void Main() + { + int num1 = 20, num2 = 5; + int addResult, subResult, mulResult; + double divResult; + Calculate(num1, num2, out addResult, out subResult, out mulResult, out divResult); + Console.WriteLine($"加法结果: {addResult}"); + Console.WriteLine($"减法结果: {subResult}"); + Console.WriteLine($"乘法结果: {mulResult}"); + Console.WriteLine($"除法结果: {divResult}"); + Console.ReadLine(); + } + static void Calculate(int a, int b, out int add, out int sub, out int mul, out double div) + { + add = a + b; // 加法 + sub = a - b; // 减法 + mul = a * b; // 乘法 + if (b != 0) + { + div = (double)a / b; + } + else + { + div = 0; + Console.WriteLine("警告:除数不能为 0,除法结果设置为 0。"); + } + } +} +*/ \ No newline at end of file diff --git "a/\345\256\236\351\252\214\344\270\200/4.cs" "b/\345\256\236\351\252\214\344\270\200/4.cs" new file mode 100644 index 0000000000000000000000000000000000000000..ec3bd2d6aaf7dc9c0fad40544603986509cd0df6 --- /dev/null +++ "b/\345\256\236\351\252\214\344\270\200/4.cs" @@ -0,0 +1,29 @@ +/* +using System; +class Program +{ + static void Main() + { + Console.WriteLine("最大值是:" + MaxValue(3, 5, 1, 8, 2)); + Console.WriteLine("最大值是:" + MaxValue(10, -2, 7)); + Console.WriteLine("最大值是:" + MaxValue(42)); + Console.ReadLine(); + } + static int MaxValue(params int[] numbers) + { + if (numbers == null || numbers.Length == 0) + { + throw new ArgumentException("至少需要提供一个数"); + } + int max = numbers[0]; + foreach (int num in numbers) + { + if (num > max) + { + max = num; // 更新最大值 + } + } + return max; + } +} +*/ \ No newline at end of file diff --git "a/\345\256\236\351\252\214\344\270\200/5.cs" "b/\345\256\236\351\252\214\344\270\200/5.cs" new file mode 100644 index 0000000000000000000000000000000000000000..c51b8e6d7d8e3d08079a801b9c1d2d8a9de01bc8 --- /dev/null +++ "b/\345\256\236\351\252\214\344\270\200/5.cs" @@ -0,0 +1,30 @@ +/* +using System; +class Program +{ + static void Main() + { + int n = 20; + long sum = CalculateFibonacciSum(n); + Console.WriteLine($"斐波那契数列前 {n} 项的和为: {sum}"); + Console.ReadLine(); + } + static long CalculateFibonacciSum(int n) + { + if (n <= 0) + { + return 0; // 如果 n 小于等于 0,返回 0 + } + long a = 1, b = 1; + long sum = a + b; + for (int i = 3; i <= n; i++) + { + long next = a + b; // 计算下一项 + sum += next; + a = b; + b = next; + } + return sum; + } +} +*/ \ No newline at end of file diff --git "a/\345\256\236\351\252\214\344\270\200/6.cs" "b/\345\256\236\351\252\214\344\270\200/6.cs" new file mode 100644 index 0000000000000000000000000000000000000000..c0f165b054979f2e28a2e9e54091d20c1607bf04 --- /dev/null +++ "b/\345\256\236\351\252\214\344\270\200/6.cs" @@ -0,0 +1,30 @@ +/* +using System; +class Program +{ + static void Main() + { + int rows = 10; + PrintPascalTriangle(rows); + Console.ReadLine(); + } + static void PrintPascalTriangle(int rows) + { + for (int i = 0; i < rows; i++) + { + // 打印每行前面的空格,使其居中 + for (int j = 0; j < rows - i - 1; j++) + { + Console.Write(" "); + } + int number = 1; + for (int j = 0; j <= i; j++) + { + Console.Write($"{number,4}"); + number = number * (i - j) / (j + 1); + } + Console.WriteLine(); + } + } +} +*/ \ No newline at end of file diff --git "a/\345\256\236\351\252\214\344\270\200/7.cs" "b/\345\256\236\351\252\214\344\270\200/7.cs" new file mode 100644 index 0000000000000000000000000000000000000000..750aa261331b407c592d821af525c24c78362caf --- /dev/null +++ "b/\345\256\236\351\252\214\344\270\200/7.cs" @@ -0,0 +1,41 @@ +/* +using System; + +class Program +{ + static void Main() + { + int[] array = { 20, 16, 78, 61, 12, 99, 57, 34, 8 }; + Console.WriteLine("排序前的数组:"); + PrintArray(array); + BubbleSort(array); + Console.WriteLine("\n排序后的数组:"); + PrintArray(array); + Console.ReadLine(); + } + static void BubbleSort(int[] arr) + { + int n = arr.Length; + for (int i = 0; i < n - 1; i++) + { + for (int j = 0; j < n - 1 - i; j++) + { + if (arr[j] > arr[j + 1]) + { + int temp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + } + } + } + } + static void PrintArray(int[] arr) + { + foreach (int num in arr) + { + Console.Write(num + " "); + } + Console.WriteLine(); + } +} +*/ \ No newline at end of file diff --git "a/\345\256\236\351\252\214\344\270\200/8.cs" "b/\345\256\236\351\252\214\344\270\200/8.cs" new file mode 100644 index 0000000000000000000000000000000000000000..fae0e982ac4a8e5cfd6d7151e67e2066d86306ba --- /dev/null +++ "b/\345\256\236\351\252\214\344\270\200/8.cs" @@ -0,0 +1,28 @@ +/* +using System; +using System.Collections.Generic; +class Program +{ + static void Main() + { + string text = "患者:“大夫,我咳嗽得很重。” 大夫:“你多大年记?” 患者:“七十五岁。” 大夫:“二十岁咳嗽吗”患者:“不咳嗽。” 大夫:“四十岁时咳嗽吗?” 患者:“也不咳嗽。” 大夫:“那现在不咳嗽,还要等到什么时咳嗽?”"; + string keyword = "咳嗽"; // 要查找的关键词 + List indices = new List(); // 存储索引位置 + int count = 0; // 统计出现次数 + int index = text.IndexOf(keyword); + while (index != -1) + { + indices.Add(index); // 记录索引位置 + count++; + index = text.IndexOf(keyword, index + keyword.Length); + } + Console.WriteLine($"“{keyword}”出现的次数: {count}"); + Console.WriteLine("每次出现的索引位置:"); + foreach (int i in indices) + { + Console.WriteLine(i); + } + Console.ReadLine(); + } +} +*/ \ No newline at end of file diff --git "a/\345\256\236\351\252\214\344\270\200/9.cs" "b/\345\256\236\351\252\214\344\270\200/9.cs" new file mode 100644 index 0000000000000000000000000000000000000000..a1a659981bebef41f2442a4fb7b21102c9fb89d5 --- /dev/null +++ "b/\345\256\236\351\252\214\344\270\200/9.cs" @@ -0,0 +1,37 @@ +/* +using System; +class Program +{ + static void Main() + { + string dateStr = "2020年10月1日"; + int year = ExtractYear(dateStr); + int month = ExtractMonth(dateStr); + int day = ExtractDay(dateStr); + Console.WriteLine($"年: {year}"); + Console.WriteLine($"月: {month}"); + Console.WriteLine($"日: {day}"); + Console.ReadLine(); + } + static int ExtractYear(string dateStr) + { + int yearEndIndex = dateStr.IndexOf('年'); + string yearStr = dateStr.Substring(0, yearEndIndex); // 截取年份部分 + return int.Parse(yearStr); + } + static int ExtractMonth(string dateStr) + { + int monthStartIndex = dateStr.IndexOf('年') + 1; + int monthEndIndex = dateStr.IndexOf('月'); + string monthStr = dateStr.Substring(monthStartIndex, monthEndIndex - monthStartIndex); // 截取月份部分 + return int.Parse(monthStr); + } + static int ExtractDay(string dateStr) + { + int dayStartIndex = dateStr.IndexOf('月') + 1; + int dayEndIndex = dateStr.IndexOf('日'); + string dayStr = dateStr.Substring(dayStartIndex, dayEndIndex - dayStartIndex); // 截取日期部分 + return int.Parse(dayStr); + } +} +*/ \ No newline at end of file