Interview Questions for "matchstick" programmers

Source: Internet
Author: User

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.

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.