Regarding the implementation of program functions in C # And thinking about code selection,

Source: Internet
Author: User

Regarding the implementation of program functions in C # And thinking about code selection,

It takes only a few days to get into contact with the C # language. What kind of in-depth research articles do you want to write? The estimation is full of conjecture and logical inferences. For the time being, starting from the introduction of language knowledge (the entry ceremony for most programmers -- output "Hello, world !") , Data and data types, data operations, program functions to achieve processes and loops, arrays and collections of these small pieces of knowledge to apply the learned knowledge in tandem, does not bear too much difficulty, for example, you are using the description language to solve the application question, and then use the code to express the statements described.

I remember that I made three questions during the course. The first question is that the master delivers grass, sheep, and wolves across the river, the wolf eats sheep, and the sheep grazes. The master does not take care of anything, but can only cross the river at a time, this question gives me the feeling that it is used to guide the way of thinking of the Students. By the way, check whether the students have a clear idea to analyze and answer the questions. The key point to finding this question is that the sheep can only exist independently or be with the masters, in this way, the problem can be solved well. The second question is that three monks and three monsters are crossing the river. There is only one empty ship. A ship can transport two people at a time. When the number of monsters exceeds the number of monks, the game is over, this question is also a way to test and think about the problem, but also pay attention to the correctness of the transport process of monsters and monks. It is necessary to ensure that the monk's safety first reaches the other side and solve the problem around this center. I was quite impressed with the third question. After that, the next student said that it could take 27 s, and then I tried again and again (only 29 s ). The requirement is to use a lamp with only 30 s of light to guide 5 people across the bridge. Only two people can hold the light at a time, the time required for the five members is 1 s, 3 s, 6 s, 8 s, and 12 s respectively. Of course, the key point of this question is to use the 1 s child as much as possible to run the light in the opposite direction, in addition, we must pass the 12 s old man and 8 s fat man. During multiple attempts, I found that as long as the 1 s child returns two trips, the 3 s child returns one, the last result is not affected regardless of the other sorting settings.

Let's review this meaningful course class ceremony and return to the world of C #. In fact, there are many similarities between the two. Now let's write about the same effect of different codes!

 

 

First, let's take a simple example. When looking for the number of daffodils, we need to solve the number of each digit of a hundred bits. 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 to answer the question. 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 all correct and implementable code. The reason why the code is different is that the problem analysis and thinking are different during calculation. The first is to discard the content after the number of digits, and then take the remaining number to 10 for the remainder, because after the removal, the remaining number of digits always correspond to the value of the number of digits. The second method is to split the number, calculate the remainder of the 10-digit number, and discard all the numbers before the number. The first one is always the value of the number, use Division to get the desired value. The third approach is to eliminate all the unnecessary values and then divide them. All in all, different ideas and solutions do not affect the implementation of the Code. However, choosing a short and elegant code can improve the beauty of the entire code. As far as your understanding is concerned, you must first select the code that you can understand, so that you can use it easily. At the same time, you can expand your knowledge and think about how to implement different ideas.

 

However, when it comes to this, some people may question what the above is a mathematical question and what is the relationship with the coding idea. Let's take a look at my different implementation ideas from other people. This question is about creating an array and assigning values, and asking users to enter a number to search, determines 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 ("this number exists in the array");} else {Console. writeLine ("this number does not exist in the array ");}

 

My approach is:

Int [] nums = {4, 8, 12,333,-9, 1}; Console. write ("Enter the number to be searched:"); int input2 = int. parse (Console. readLine (); for (int I = 0; I <5; I ++) {if (nums [I] = input2) Console. writeLine ("this value is found in the array, which is the number of" + (I + 1) + "in the array! "); If (I = 4 & nums [I]! = Input2) Console. WriteLine ("the corresponding item is not found in the array! ");}

 

The first code is to define a bool-type data isFind. If it is found, the isFind data is changed and then implemented through the isFind data. When I was thinking about it, I thought that if I didn't find it, the number of cycles would reach the maximum value after the loop is completed, but at this time the last digit is the same as the number of inputs, the corresponding conditions of both outputs can be met. Therefore, the final result of troubleshooting and the value of the last digit cannot be met. Through this analysis, the two pieces of code are written. This is how different ideas use different code to implement the same function.

 

There is another classic example about how different codes implement the same function. This is the sorting of arrays and sets. The following three ideas are introduced: exchange sorting, Bubble sorting, and select sorting.

The idea of switching the sorting center is to start from the first array item, fix nums [I], and compare the data after I + 1 in sequence, if there is a value smaller than num [I], it is exchanged.

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;         }   }}

 

Bubble Sorting refers to sinking the largest number to the bottom. First, the last position is fixed and then compared from the first number. Each time a large number is encountered, this number is exchanged with the next one, just like a bubble. The value in the search for this change is larger and larger until the last one. At this time, confirm the second to last and replace it again. (In the second for loop, the value of nums [j] always increases gradually for each loop .) The implementation code is as follows:

 

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;            }   }}

 

Select sorting from the first number. Assume that the first number is the smallest number and compare it with the next number. If there is a small number, record the subscript Of this number. After the loop is completed, the number of subscripts corresponding to the record must be the minimum value of the Data Group. At this time, replace the minimum value with the first value. It is followed by a loop to complete sorting.

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 three sorting methods show that as long as the functions are implemented, the idea and Code are not important. As long as you can find the key points to solve the problem, and understand the solution to the problem around the key points, determine the process according to the method, and then write the code, so as to achieve the implementation of the function is not difficult. However, for the convenience of viewing and modifying the entire code, when using the code, you should try to use statements with excellent structure and concise statements while understanding the code writing ideas. Of course, if some methods are hard to understand, you 'd better use the code you understand to facilitate your own viewing and modification. If necessary, annotations are also essential.

All in all, there is always no harm in looking at others' ideas and looking more at development. After all, it is programming, hard to understand or use, and there is no other shortcut.

The future has come!

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.