Thinking about the implementation of program function and the choice of code in C #

Source: Internet
Author: User

Contact C # language only a few days time, want to write what tall on the deep Research article, estimate is full of speculation and some illogical inference. As of now, from the language primer (most programmers of the introductory ceremony-output "hello,world! "), data and data types, data operations, program functions to implement processes and loops, arrays and collections of these chunks of knowledge to apply the knowledge in tandem, and do not suffer too much difficulty, such as in the description of the language to solve the application problem, and then through the code to express these statements.

  Remember to do three fun questions during the course. The first problem is the owner of the transport of grass, sheep and wolves across the river, wolves eat sheep, sheep eat grass, the host of the situation will not happen anything, one can only transport the same river, the problem to me the feeling is used to guide students thinking, by the way to observe whether students have a clear idea to analyze and answer questions, Finding the key point is that the sheep can only exist alone or with the owner, so that the problem can be solved well. The second problem is three monks and three monsters across the river, only an empty boat, the ship can be transported two people, whenever the number of monsters more than the number of monks, the game is over, the problem is also a test of the way of thinking, but also to pay attention to the monster and monk transport process correctness, must ensure that the monk safe to reach the other side Around the center to solve the problem. The third question impression is more profound, after finished, next to the classmate said can 27s, then I have made repeated attempts (only 29s). Request is the use of a light can only light 30s to guide 5 people cross the bridge, only two people holding the light through, 5 people need time for 1s, 3s, 6s, 8s, 12s of course, this problem to grasp the focus, is to try to use the 1s children to carry the lights in the opposite direction, and must be 12s of the elderly and the fat of 8s In a number of attempts, I found that as long as the 1s child returned two times, 3s children returned to a trip, the longest time to pass the two-bit, regardless of how the rest of the sorting settings, will not affect the final result.

Looking back at this meaningful start-up ceremony, and then back to the world of C #, there are a lot of things in common, and now it's time to write the same implementation effect of different code!

To give a simple example, in order to find the number of daffodils, you need to solve the value of each bit of a number of hundred, the code given in the answer is this: 


int i = 100;while (i <) {    int a = i/100%;    int b = I/10%;    int c = i%;    if (A * a * a + b * b * b + c * c * c = i)    {        Console.WriteLine (i);    }    i++;}

And I used two different methods for the answer, the first of which is:


int i = 100;while (i <) {    int a = i/100;    int b = i% 10/10;    int c = i%;    if (A * a * a + b * b * b + c * c * c = i)    {        Console.WriteLine (i);    }    i++;}

The second method is:


int i = 100;while (i <) {    int a = i/100;    int B = (i  -A * +)/ten;    int c = i-a * 100-b *;    if (A * a * a + b * b * b + c * c * c = i)    {        Console.WriteLine (i);    }    i++;}

The above are the correct, achievable code, the code is different, that is, in the operation, the problem analysis and the angle of thinking. The first is to discard the content after the desired number of digits, and then take the remaining number to 10, because the number of bits remaining after the cut always corresponds to the value of the number of digits to be calculated. The second is also to divide this number, the number of the desired number of bits of a multiple of 10 to the remainder, the number of digits before the total discard, get the first bit is always the value of the number of digits, and then use division to get the desired value. The third is a way to die, and all the extra minus will be removed. All in all, different ideas, different ways of solving the problem will not affect the implementation of the code, but the choice of short, elegant code, can improve the beauty of the entire code, this still needs to be noted. In terms of their own perception, it is definitely preferred to choose their own understanding of the code, so that it will be handy, but also to expand their own insight, think of different ways of realizing the way.

But here, perhaps some people will be questioned, above is a mathematical problem, and the idea of coding has something to do with it. Then look at me and the rest of the different implementation of the idea, the question is about creating an array and assigning values, Let the user enter a number to find, to determine whether the number exists in the array.


Int[] Nums = {4, 8, 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 ("The number exists in the array");} else{    Console.WriteLine ("The number does not exist in the array");}

The way I think about it is:


Int[] Nums = {4, 8, 12, 333,-9, 1}; Console.Write ("Enter the number you want to find:"); int input2 = Int. Parse (Console.ReadLine ()), for (int i = 0; i < 5; i++) {   if (nums[i] = = Input2)       Console.WriteLine ("Finds the value in the array, the Number of "+ (i + 1) +" items in the array! ");   if (I==4&&NUMS[I]!=INPUT2)       Console.WriteLine ("The corresponding item is not found in the array!") "); }

The first code is by defining a bool type data isfind, if found, changes the Isfind data, and then completes the implementation through the Isfind data. And I think, if not found, then the loop will be completed after the number of cycles to reach the maximum, but at this time the last digit and the number of inputs are the same, two output corresponding conditions can be satisfied, so, the final and the last one of the value will not meet the output can not find results. With this analysis, the two pieces of code are written. This is how different ideas use different codes to achieve the same functionality.

There is one more classic example of how different code implements the same functionality, and that is the sort of arrays and collections, which are described in three ways: Swap sort, bubble sort, and select sort.

The central idea of exchange sorting is to start with the first array item, fix nums[i], then compare the data after i+1, and if there is a value smaller than num[i], swap it.


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 sort is to sink the largest number to the bottom, first fixed the last position, and then from the first number to compare, each encountered a large number, this number with the latter exchange, like a bubble, the change in the search for the value of the more rolling, until the last one. Then, make sure the second-to-last position is replaced again. ( in the second for loop, the value for each loop,Nums[j] is always getting larger. 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 sort starting from the first number, first assume that the first number is the smallest number, compare it with each subsequent number, if you encounter a small, record the subscript of this number, after the loop is complete, the record subscript corresponding to the number must be the minimum value of the data group, at this time replace the minimum value to the first bit. Loop back, and finish 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;}

There are three kinds of sorting methods can be seen, as long as the ability to implement functions, ideas and code is not important. As long as you can find the key to solve the problem, and around the key points to understand the problem-solving method, according to the method to determine the process, and then complete the code to write, so to achieve the realization of the function is not difficult. However, for the whole code to be easy to view and modify, in the use of these code, in the understanding of the code to write the idea of the premise, try to use the structure of good, concise statement. Of course, if some methods are difficult to understand, it is best to use your own understanding of the code to write, easy to complete the review and modification, if necessary, comments are also necessary.

In a word, it is always no harm to observe other people's ideas and see how much they want to develop. After all, it is programming, difficult to understand or use unskilled, the solution is to practice more knocking, no other shortcut.

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.