1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
namespace 字符串测试1 { internal class Program {
public static void Fun(string[] Arr) { Arr[0] = "Super Big"; } static void Main(string[] args) { string[] names = { "Big", "Head", "Fish" }; Fun(names); Console.WriteLine($"{names[0]} {names[1]} {names[2]}"); Console.WriteLine("Hello, World!"); } } }
|
课堂上老师还布置了一个任务,讲就是让我们用一个循环,按要求输出字符串数组。我们知道,数组中的数据存储方式是连续存储的。所以只要我们得到字符串的起始地址,我们从这里开始遍历,遍历的次数就是数组的行数乘列数。当我们遍历完一行就可以输出换行进行分割。这样就用一个循环完成了要求。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| namespace 按要求输出 { internal class Program { unsafe static void Main(string[] args) { int j = 0; string[,] myArr = { { "A", "B", "C" }, { "D", "E", "F" }, { "G", "H", "I" } }; fixed (string* p = myArr) { string *temp = p; for (int i = 0; i < myArr.GetLength(0) * myArr.GetLength(1); i++) { Console.Write($"{*temp} "); temp++; if ((i + 1) % 3 == 0) { Console.WriteLine(); } } } } } }
|
这个任务主要考察对字符串分割的函数的运用。StringObj.Split(SplitCh) Splitch 为分割的字符,这个函数可以按 Splitch 进行分割。而这个函数反馈的是一个数组,所以我们需要定义一个数组去存储函数返回的数据。最后我们对这个数组进行遍历,就能完成任务。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| namespace 字符串的分割 { internal class Program { static void Main(string[] args) { string s1 = "Central-South-University"; string[] s2 = s1.Split('-'); for (int i = 0; i < s2.Length; i++) { Console.WriteLine(s2[i]); } Console.WriteLine("Hello, World!"); } } }
|
这里我也把上次布置的作业解答进行分享。上次布置的代码要求我们输出 4 位水仙花数,我觉得求水仙花数的思路大致一致,所以写了一个求 n 位水仙花数的代码。考虑到在输入阶段可能会出现一些错误,我在代码中加入了一些判断和递归来规避错误。希望我的代码能提供一些帮助。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| using System; namespace _4位水仙花数 { internal class Program { public static int Cal(int num, int power) { int ans = 0; int temp; for (int i = 0; i < power; i++) { temp = num % 10; ans += (int)Math.Pow(temp, power); num /= 10; } return ans; } public static void Find_Num_Proc(int start, int end, int power) { int ans = 0; if (start >= end) { Console.WriteLine("输入的数据范围有误,请重新输入"); Console.Write("输入起始值:"); start = Convert.ToInt32(Console.ReadLine()); Console.Write("输入结束值:"); end = Convert.ToInt32(Console.ReadLine()); Console.Write("输入计算的水仙花数位数:"); power = Convert.ToInt32(Console.ReadLine()); Find_Num_Proc(start, end, power); return; } if ((int)Math.Pow(10, power - 1) > start) { Console.WriteLine($"由于您想要求得是位数为{power}的水仙花数\n而您输入的数字范围的最小值要超过它的限制,所以这里我们为您实际求得范围为{(int)Math.Pow(10, power - 1)}到{end}"); start = (int)Math.Pow(10, power - 1); } if ((int)Math.Pow(10, power) - 1< end) { Console.WriteLine($"由于您想要求得是位数为{power}的水仙花数\n而您输入的数字范围的最大值要超过它的限制,所以这里我们为您实际求得范围为{start}到{(int)Math.Pow(10,power) - 1}"); end = (int)Math.Pow(10, power) - 1; } Console.WriteLine("------------------------------------------------"); for (int i = start; i <= end; i++) { if (Cal(i, power) == i) { Console.WriteLine($"{i}"); ans++; } } Console.WriteLine($"在范围内共找到了{ans}个水仙花数"); } static void Main(string[] args) { int start, end, power; Console.WriteLine("这是一个计算多位水仙花数的小工具 :)"); Console.Write("输入起始值:"); start = Convert.ToInt32(Console.ReadLine()); Console.Write("输入结束值:"); end = Convert.ToInt32(Console.ReadLine()); Console.Write("输入计算的水仙花数位数:"); power = Convert.ToInt32(Console.ReadLine()); Find_Num_Proc(start, end, power);
} } }
|