A pair of matching questions

Source: Internet
Author: User

Do you still remember the question of adding or moving a match when I was a child to establish the equation? Hey, today I will share with you an interview question, which is very similar to the match question, so it is more like a quiz question. It is very interesting to do O (answer _ Answer) O. If you don't talk much about it, just go to the question:

/*

The following section of the C program will output 20 minus signs. However, the careless programmer writes the code wrong and you need to modify the following code correctly. However, you can only add or modify one character. Please give three answers.

*/

Int n = 20;

For (int I = 0; I <n; I --)

{Printf ("-");

}

The question is very simple. I believe that any programmer can understand it. This question does not involve complicated professional knowledge. Just do it when you answer the question. Pay attention to the question and ask for "three answers ", it's easy to leak it out.

The following is my solution:

After a simple analysis of the program, it is not difficult to see that "Careless programmers" write I ++ as I --, resulting in a loop that cannot be executed 20 times. Therefore, our ultimate goal is to make the for Loop Run 20 times. Only one character can be added or modified. In this way, only "int n = 20" and "for (int I = 0; I <n; i. A simple attempt to modify the n value quickly found that it would not work, so the key to solving the problem is to work hard in the for loop conditions.

First we try to modify the I value in "int I = 0", because it is obviously impossible to meet both I <20 and I-20> = 20, that is, 40 <= I <20.

Next, we try to modify the conditional expression "I <n" and think about it for a moment. We find that if we add a symbol before I, it becomes "-I <n ", that is, "I>-20", the order of each loop is 0>-20,-1>-20... -19>-20: the task can be run 20 times! In this way, the first solution comes out:

1 int n = 20;

2 for (int I = 0;-I <n; I --)

3 {

4 printf ("-");

5}

Let's take a look at the hope of modifying "I --". It is obviously not possible to change it to I ++. How about changing the variable? N --? It's okay! The condition for each loop is 0 <20, 0 <19... 0 <1. It is also 20 cycles, which is amazing! This tells us that for (int I = 0; I <n; n --) can also do n cycles, although we almost do not write this, because n is always immutable (such as the length of a specific array and the size of a container ). Solution 2:

1 int n = 20;

2 for (int I = 0; I <n; n --)

3 {

4 printf ("-");

5}

In general, the first two solutions are quite easy to think of, and the third one is about to go around. I also thought about it for a long time (stupid o (> stupid <) o ). Return to modifying "I <n". Since no 0 is true in C, any int value can be used as a Boolean value, therefore, we can try to replace "I <n" with a numerical value, for example, I + n, I-n, I * n, and so on, while I + n exactly fits the meaning of the question (very strange to say ~) Solution 3:

1 int n = 20;

2 for (int I = 0; I + n; I --)

3 {

4 printf ("-");

5}

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.