Author: Chen Hao, original address: http://coolshell.cn/articles/3961.html
Sometimes, some interview questions are very boring. No, there is another one. Remember the "matchstick game" that I played when I was a child, that is, moving a matchstick to change a figure or word.ProgramThe interview can also be done like this. Let's take a look at the following matchstick program interview questions.
The following is a C program that wants to output 20 minus signs. However, careless programmersCodeIf it is wrong, you need to modify the following code correctly. However, you can only add or modify one of the characters. Please give three answers.
Int n = 20; For (INT I = 0; I <n; I --) {printf ("-");}
Don't think this question is not very difficult. I believe you are not so easy to find three methods. I think if you can find these three methods within 10 minutes, it means you are really smart and responsive. Of course, it does not matter within 15 minutes. However, if you cannot find three methods within 30 minutes, it doesn't mean you are stupid. At most, your response is not fast enough. Hey. Just play.
Below is my answer:
// Solution 1: Add a negative sign for (INT I = 0; I <-N; I --) to N in the for loop. // solution 2: initialize n to-20int n =-20; // solution 3: Initialize I in the for loop to 40for (INT I = 40; I <n; I --)
However, I want to tell you that none of the above answers is correct (I will know that you will peek at the answers). However, these ideas are very similar. Haha.
The following is the correct answer --
// Solution 1: Add a negative sign for (INT I = 0;-I <n; I --) to I in the for loop. // solution 2: in the for loop, convert I -- to n -- for (INT I = 0; I <n; n --) // The third solution: convert <in the for loop to + for (INT I = 0; I + N; I --)
Other related variants are as follows:
Modify or add a character to Output 21 minus signs
Modify or add a character so that only one minus sign is output.
Modify or add a character so that it does not output a minus sign.