Home >Backend Development >C#.Net Tutorial >Thoughts on program function implementation and code selection in C#
I have only been exposed to the C# language for a few days. If I want to write any high-level in-depth research article, it is probably full of conjectures. and some illogical inferences. So far, from the introductory knowledge of the language (the introductory ritual of most programmers - outputting "Hello, world!"), data and data types, data operations, program function implementation processes and loops, arrays and sets, these are divided into blocks. From the small knowledge point to applying the learned knowledge in series, I did not have to bear too many difficulties, such as using descriptive language to solve application problems, and then expressing these descriptive statements through code.
I remember doing three interesting questions at the beginning of the class. The first question is about the owner transporting grass, sheep and wolves across the river. The wolves eat the sheep and the sheep eat grass. Nothing will happen if the owner takes care of the situation. He can only transport one thing across the river at a time. This question gives me the feeling that it is used to guide The way students think, by the way, observe whether students have a clear idea to analyze and answer problems, and find the key point that sheep can only exist alone or with their owners, so that the problem can be solved well. The second question is about three monks and three monsters crossing the river. There is only one empty boat. The boat can transport two people at a time. Whenever the number of monsters is greater than the number of monks, the game is over. This question is also a test of how to think about the problem. , but we must also pay attention to the correctness of the transportation process of monsters and monks. We must ensure that all monks reach the other side safely first, and solve the problem around this center. The third question left a deep impression on me. After I finished it, the classmate next to me said it could take 27 seconds, and then I tried again and again (it could only take 29 seconds). The requirement is to use a light that can only shine for 30 seconds to guide 5 people across the bridge. Only two people can pass with the light at a time. The time required for the 5 people is 1s, 3s, 6s, 8s, and 12s. Of course, this question must be grasped The key point is to try to use the 1s child to run the light in the opposite direction, and the 12s old man and the 8s fat man must be passed in one trip. After many attempts, I found that as long as the 1s child returns twice, the 3s child returns once In one pass, the two digits that take the longest time are passed through at once. No matter how the other sorting settings are set, it will not affect the final result.
Looking back at this meaningful opening ceremony, and returning to the world of C#, in fact, the two have a lot in common. Now let’s write the same implementation effect of different codes. One thing!
Let’s take a simple example first. When calculating the number of daffodils, we need to add a hundred digit Solve for the numerical value of each digit. The code given in the answer is as follows:
int i = 100;while (i < 1000) { int a = i / 100 % 10; int b = i / 10 % 10; int c = i % 10; if (a * a * a + b * b * b + c * c * c == i) { Console.WriteLine(i); } i++; }
I used two different methods when answering. The first one is:
##
int i = 100;while (i < 1000) { int a = i /100; int b = i % 10 / 10; int c = i % 10; if (a * a * a + b * b * b + c * c * c == i) { Console.WriteLine(i); } i++; }
The second method is:
int i = 100;while (i < 1000) { int a = i / 100; int b = (i - a * 100) / 10; int c = i -a * 100 - b * 10; if (a * a * a + b * b * b + c * c * c == i) { Console.WriteLine(i); } i++; }
The above are correct and achievable codes. The reason why the code is The difference lies in the different perspectives of analyzing and thinking about problems when doing calculations. The first is to discard the content after the number of digits sought, and then take the remainder of the number to 10, because the ones digit of the remaining number after excision always corresponds to the value of the number of digits sought. The second method is also to split the number, find the remainder of the number that is a multiple of 10 of the number of digits sought, and discard all the numbers before the number of digits sought. The first digit obtained is always the number of digits sought. value, and then use division to get the desired value. The third method is a dead end, just cut off all the excess and then eliminate it. All in all, different ideas and problem-solving methods will not affect the implementation of the code, but choosing short and elegant code can improve the beauty of the entire code, which still needs to be noted. As far as my own insights are concerned, I must give priority to choosing codes that I can understand, so that I can use them easily. At the same time, I should expand my knowledge and think more about how to implement different ideas.
However, when it comes to this, some people may question that the above is a math problem and what it has to do with the idea of coding. Then let’s take a look at the different implementation ideas between me and others. This question is about creating an array and assigning values. Let the user enter a number to be found to determine whether the number exists in the array.
int[] nums = { 4, 8, 12, 333, -9, 1 };bool isFind = false;for (int i = 0; i < nums.Length; i++) { if (nums[i] == n) { isFind = true; break; } }if (isFind) { Console.WriteLine("数组中存在该数"); }else{ Console.WriteLine("数组中不存在该数"); }
The way I think about it is:
int[] nums = { 4, 8, 12, 333, -9, 1 }; Console.Write("请输入需要查找的数字:");int input2 = int.Parse(Console.ReadLine());for (int i = 0; i < 5; i++) { if (nums[i] == input2) Console.WriteLine("在数组中查找到该数值,该数为数组中的第" + (i + 1) + "项!"); if(i==4&&nums[i]!=input2) Console.WriteLine("未在数组中找到对应项!"); }
第一种代码是通过定义一个bool类型数据isFind,如果找到,就改变isFind的数据,然后通过isFind的数据完成实现。而我在思考时,是想如果没有找到,那么循环完成后循环次数就会达到最大值,但是此时最后一位数与输入的数相同,两个输出对应条件都能满足,所以,排查到最后并且最后一位的值也不等,才能满足输出未找到结果。通过这样的分析,就写出了这两段代码。这就是不同思路采用不同代码来实现相同功能的方式。
关于不同代码实现相同功能,还有一个最经典的例子,是不能不提的,那就是数组和集合的排序,下面介绍三种思路:交换排序、冒泡排序和选择排序。
交换排序中心思想是从第一个数组项开始,固定nums[i],依次第i+1个后面的数据进行比较,如果有比num[i]小的值,就对其进行交换。
for( int i = 0; i < arrays.Length - 1; i++) { for(int j = i+1; j < arrays.Length; j++) { if(arrays[i]>arrays[j]) { int temp=arrays[i]; arrays[i]=arrays[j]; arrays[j]=temp; } } }
冒泡排序是将最大的数沉到底部,先将最后一个位置固定,再从第一个数开始比较,每遇到一个大的数,这个数就与后一位交换,就像气泡一样,这个变动的寻找中的值越滚越大,直到最后一位。这时,再确定倒数第二位,再次进行替换。(第二个for循环中,每次循环,nums[j]的值总是逐渐变大。)实现代码如下:
for(int i = nums.Length - 1; i > 0; i--) { for(int j = 0; j < i; j++) { if( nums[j] > nums[j+1] ) { int temp = nums[j]; nums[j] = nums[j+1]; nums[j+1] = temp; } } }
选择排序从第一个数开始,先假设第一个数为最小的数,将其与后面每一个数进行比较,如果遇到小的,就记录这个数的下标,循环完成后,记录的下标对应的数一定是数据组的最小值,此时替换最小值到第一位。后面依次循环,完成排序。
for(int i = 0; i < nums.Length - 1; i++) { int index = 1; for(int j = i+1; j < nums.Length; j++) { if(nums[j])<nums[index]) { index=j; } } int temp = nums[i]; nums[i] = nums[index]; nums[index] = temp; }
有上面三种排序方法可以看出,只要能够实现功能,思路和代码并不重要。只要能找到解决问题的关键点,并围绕关键点弄懂解决问题的方法,根据方法确定流程,再完成代码的编写,这样想要达到功能的实现并不难。不过为了整个代码的便于查看和修改,在使用这些代码时,在能够理解代码书写的思路前提下,尽量使用结构优良,语句简洁的语句。当然,如果一些方法难以理解,最好还是使用自己理解的代码书写,便于自己完成查看和修改,如果必要,注释也是必不可少。
总而言之,多观察别人的思路,多看多想多开拓,总是没有坏处。毕竟是编程,难以理解或者使用不熟练,解决的方法还是多练多敲,没有其他的捷径。
The above is the detailed content of Thoughts on program function implementation and code selection in C#. For more information, please follow other related articles on the PHP Chinese website!