diff --git a/ConsoleApp1.sln b/ConsoleApp1.sln new file mode 100644 index 0000000000000000000000000000000000000000..9f9185ba0dd10a2576675456fab6b46119ac8b6a --- /dev/null +++ b/ConsoleApp1.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.7.34031.279 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp1", "ConsoleApp1.csproj", "{CE2106D1-549D-4AAF-B37D-C12B806EDD63}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {CE2106D1-549D-4AAF-B37D-C12B806EDD63}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CE2106D1-549D-4AAF-B37D-C12B806EDD63}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CE2106D1-549D-4AAF-B37D-C12B806EDD63}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CE2106D1-549D-4AAF-B37D-C12B806EDD63}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {022CE94C-C641-4773-8B12-57921299962D} + EndGlobalSection +EndGlobal diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000000000000000000000000000000000000..c448b490c1b4e3a3fd09f54c09bbb3be521935b0 --- /dev/null +++ b/Program.cs @@ -0,0 +1,2 @@ + +Console.WriteLine("Hello, World!"); diff --git "a/\344\270\273\345\207\275\346\225\260.cs" "b/\344\270\273\345\207\275\346\225\260.cs" new file mode 100644 index 0000000000000000000000000000000000000000..61c0407f5af08ba654c325cd4d1d0742d6d786da --- /dev/null +++ "b/\344\270\273\345\207\275\346\225\260.cs" @@ -0,0 +1,205 @@ +using System; +using System.Text.RegularExpressions; + +class Utility +{ + // 交换两个变量 + public static void Swap(ref int firstNumber, ref int secondNumber) + { + firstNumber = firstNumber + secondNumber; + secondNumber = firstNumber - secondNumber; + firstNumber = firstNumber - secondNumber; + } + + // 计算加、减、乘、除 + public static void Calculate(int firstNumber, int secondNumber, out int sum, out int difference, out int product, out int quotient) + { + sum = firstNumber + secondNumber; + difference = firstNumber - secondNumber; + product = firstNumber * secondNumber; + quotient = firstNumber / secondNumber; + } + + // 计算任意多个数的最大值 + public static int Max(params int[] numbers) + { + int maxValue = numbers[0]; + foreach (int number in numbers) + { + if (number > maxValue) maxValue = number; + } + return maxValue; + } + + // 冒泡排序 + public static void Sort(int[] array) + { + int length = array.Length; + for (int i = 0; i < length - 1; i++) + { + for (int j = 0; j < length - i - 1; j++) + { + if (array[j] > array[j + 1]) + { + int temp = array[j]; + array[j] = array[j + 1]; + array[j + 1] = temp; + } + } + } + } + + // 格式化字符串 + public static string Format(string input) + { + input = input.Trim(); + input = Regex.Replace(input, @"\s+", " "); + return input; + } +} + +class Program +{ + static void Main(string[] args) + { + // 1. 交换两个变量 + Console.WriteLine($"第一题,输入回车继续"); + string input = Console.ReadLine(); + int number1 = 10, number2 = 20; + Console.WriteLine($"交换前: number1 = {number1}, number2 = {number2}"); + number1 = number1 + number2; + number2 = number1 - number2; + number1 = number1 - number2; + Console.WriteLine($"交换后: number1 = {number1}, number2 = {number2}"); + + // 2. 使用 ref 方法交换 + Console.WriteLine($"第二题,输入回车继续"); + input = Console.ReadLine(); + Console.WriteLine($"交换前: number1 = {number1}, number2 = {number2}"); + Utility.Swap(ref number1, ref number2); + Console.WriteLine($"交换后: number1 = {number1}, number2 = {number2}"); + + // 3. 计算加、减、乘、除 + Console.WriteLine($"第三题,输入两个数字用回车分开"); + int sum, difference, product, quotient; + int firstNumber = int.Parse(Console.ReadLine()); + int secondNumber = int.Parse(Console.ReadLine()); + Utility.Calculate(firstNumber, secondNumber, out sum, out difference, out product, out quotient); + Console.WriteLine($"计算: 和 = {sum}, 差 = {difference}, 积 = {product}, 商 = {quotient}"); + + // 4. 计算任意多个数的最大值 + Console.WriteLine($"第四题"); + Console.WriteLine("请输入五个整数,用回车分隔:"); + int num1 = int.Parse(Console.ReadLine()); + int num2 = int.Parse(Console.ReadLine()); + int num3 = int.Parse(Console.ReadLine()); + int num4 = int.Parse(Console.ReadLine()); + int num5 = int.Parse(Console.ReadLine()); + int maxValue = Utility.Max(num1, num2, num3, num4, num5); + Console.WriteLine($"最大值: {maxValue}"); + + // 5. 斐波那契数列前20项的和 + Console.WriteLine($"第五题,输入回车继续"); + input = Console.ReadLine(); + int a = 0, b = 1, sumFibonacci = 0; + for (int i = 0; i < 20; i++) + { + sumFibonacci += a; + int temp = a; + a = b; + b = temp + b; + } + Console.WriteLine($"斐波那契数列前20项的和: {sumFibonacci}"); + + // 6. 打印杨辉三角形 + Console.WriteLine($"第六题,输入回车继续"); + input = Console.ReadLine(); + Console.WriteLine("杨辉三角形:"); + for (int i = 0; i < 10; i++) + { + int value = 1; + for (int j = 0; j <= i; j++) + { + Console.Write(value + " "); + value = value * (i - j) / (j + 1); + } + Console.WriteLine(); + } + + // 7. 冒泡排序 + Console.WriteLine($"第七题,输入回车继续"); + input = Console.ReadLine(); + int[] array = { 20, 16, 78, 61, 12, 99, 57, 34, 8 }; + Utility.Sort(array); + Console.WriteLine("冒泡排序结果: " + string.Join(", ", array)); + + // 8. 统计字符串中“咳嗽”的出现次数及索引位置 + Console.WriteLine($"第八题,输入回车继续"); + input = Console.ReadLine(); + string text = "患者:“大夫,我咳嗽得很重。” 大夫:“你多大年记?” 患者:“七十五岁。” 大夫:“二十岁咳嗽吗”患者:“不咳嗽。” 大夫:“四十岁时咳嗽吗?” 患者:“也不咳嗽。” 大夫:“那现在不咳嗽,还要等到什么时咳嗽?”"; + string keyword = "咳嗽"; + int count = 0; + int index = -1; + + while ((index = text.IndexOf(keyword, index + 1)) != -1) + { + count++; + Console.WriteLine($"Found '{keyword}' at index: {index}"); + } + + Console.WriteLine($"Total occurrences of '{keyword}': {count}"); + + // 9. 从日期字符串中提取年、月、日 + Console.WriteLine($"第九题,输入回车继续"); + input = Console.ReadLine(); + 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: {year}, Month: {month}, Day: {day}"); + + // 10. 格式化字符串 + Console.WriteLine($"第十题,输入回车继续"); + input = Console.ReadLine(); + string inputStr = " Hello World ! 你好 世界 ! "; + string formattedStr = Utility.Format(inputStr); + Console.WriteLine($"格式化后的字符串: \"{formattedStr}\""); + + // 11. 猜拳游戏 + Console.WriteLine($"第十一题,输入回车继续"); + input = Console.ReadLine(); + Random random = new Random(); + string[] choices = { "石头", "剪刀", "布" }; + + while (true) + { + Console.WriteLine("请输入你的选择(石头、剪刀、布):"); + string playerChoice = Console.ReadLine(); + int computerChoice = random.Next(0, 3); + + Console.WriteLine($"你选择了:{playerChoice}"); + Console.WriteLine($"电脑选择了:{choices[computerChoice]}"); + + if (playerChoice == choices[computerChoice]) + { + Console.WriteLine("平局!"); + } + else if ((playerChoice == "石头" && choices[computerChoice] == "剪刀") || + (playerChoice == "剪刀" && choices[computerChoice] == "布") || + (playerChoice == "布" && choices[computerChoice] == "石头")) + { + Console.WriteLine("你赢了!"); + } + else + { + Console.WriteLine("你输了!"); + } + + Console.WriteLine("再来一局?(y/n)"); + if (Console.ReadLine().ToLower() != "y") + { + break; + } + } + } +} \ No newline at end of file diff --git "a/\351\202\223\347\232\223\346\226\207/\345\256\236\351\252\2141.cs" "b/\351\202\223\347\232\223\346\226\207/\345\256\236\351\252\2141.cs" new file mode 100644 index 0000000000000000000000000000000000000000..15d0f914f00ef7e88ac63cfaec997d16b4d9cf97 --- /dev/null +++ "b/\351\202\223\347\232\223\346\226\207/\345\256\236\351\252\2141.cs" @@ -0,0 +1,208 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Xml.Serialization; +class program +{ + static void Test01(){ + int x=int.Parse(Console.ReadLine()); + int y=int.Parse(Console.ReadLine()); + x = x + y; + y = x - y; + x = x - y; + + Console.WriteLine("x={0},y={1}",x,y); +} + static void Test02(ref int x,ref int y) + { + x = x + y; + y = x - y; + x = x - y; + } + static void Test03(int a, int b, out int sum, out int difference, out int product, out double quotient) + { + sum = a + b; + difference = a - b; + product = a * b; + if (b != 0) + { quotient = (double)a / b; } + else { quotient = double.NaN; } + } + + static void Test04() + { + + int max = FindMax(10, 20, 5, 30, 15); + + + Console.WriteLine("Test04 - 最大值: {0}", max); + } + + + static int FindMax(params int[] numbers) + { + if (numbers.Length == 0) + { + Console.WriteLine("至少需要提供一个参数"); + } + + int max = numbers[0]; + foreach (int num in numbers) + { + if (num > max) + { + max = num; + } + } + return max; + } + static void Test05() + { + + int sum = fbnq(20); + + + Console.WriteLine("Test05 - 斐波那契数列前20项的和: {0}", sum); + } + + + static int fbnq(int n) { + + if (n <= 0) + { + throw new ArgumentException("n 必须大于 0"); + } + + 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; + } + static void Test06() + { + int N = 10; + int[,] arr = new int[N,N]; + for (int i = 0; i < N; i++) + { + arr[i, 0] = 1; + arr[i, i] = 1; + } + for (int i = 2; i < N; i++) + { + for (int j = 1; j < i; j++) + { + arr[i, j] = arr[i - 1, j - 1] + arr[i - 1, j]; + } + } + for (int i = 0; i < N; i++) + { + + for (int j = 0; j < N - i - 1; j++) + { + Console.Write(" "); + } + + + for (int j = 0; j <= i; j++) + { + Console.Write(arr[i, j] + " "); + } + + + Console.WriteLine(); + } + } + static void My_sort(int[] arr,int l,int r) + { if (l >= r) return; + int x=arr[(l+r)/2]; + int i = l - 1; + int j = r + 1; + while (i x); + if (i < j) Test02(ref arr[i], ref arr[j]); + } + My_sort(arr,l,j); + My_sort(arr,j+1,r); + } + static void Test07() + { + int[] arr = { 20, 16, 78, 61, 12, 99, 57, 34, 8 }; + My_sort(arr,0,8); + foreach(int i in arr) + { + Console.WriteLine(i); + } + } + static void Test08() + { + Listarr=new List(); + Listposition = new List(); + arr.Add("患者:“大夫,我咳嗽得很重。” 大夫:“你多大年记?” 患者:“七十五岁。” 大夫:“二十岁咳嗽吗”患者:“不咳嗽。” 大夫:“四十岁时咳嗽吗?” 患者:“也不咳嗽。” 大夫:“那现在不咳嗽,还要等到什么时咳嗽?"); + string keyword = "咳嗽"; + int index = 0; + while ((index = arr[0].IndexOf(keyword,index)) != -1) + { + position.Add(index); + index += keyword.Length; + } + Console.WriteLine($"关键字 '{keyword}' 出现了 {position.Count} 次。"); + Console.WriteLine("出现的位置如下:"); + foreach (int pos in position) + { + Console.WriteLine($"位置: {pos}"); + } + } + static void Test09() + { + string dateStr = "2020年10月1日"; + int yearIndex = dateStr.IndexOf("年"); + int monthIndex = dateStr.IndexOf("月"); + int dayIndex = dateStr.IndexOf("日"); + string year = dateStr.Substring(0, yearIndex); + string month = dateStr.Substring(yearIndex + 1, monthIndex - yearIndex - 1); + string day = dateStr.Substring(monthIndex + 1, dayIndex - monthIndex - 1); + Console.WriteLine($"年: {year}"); + Console.WriteLine($"月: {month}"); + Console.WriteLine($"日: {day}"); + } + static void Test10() + { + string input = " Hello World ! 你好 世界 ! "; + string trimmed = input.Trim(); + StringBuilder result = new StringBuilder(); + bool pre = false; + + foreach (char c in trimmed) + { + if (c == ' ') + { + if (!pre) + { + result.Append(c); + pre = true; + } + } + else + { + result.Append(c); + pre = false; + } + } + Console.WriteLine($"原始字符串: \"{input}\""); + Console.WriteLine($"处理后的字符串: \"{result.ToString()}\""); + } + + static void Main(string[] args) + { + Test08(); + } +}