文章来源:
再谈“我是怎么招聘程序员的”
http://blog.csdn.net/haoel/archive/2011/04/21/6338025.aspx
“火柴棍式”程序员面试题
http://coolshell.cn/articles/3961.html/comment-page-45#comment-58598
题目如下:
下面是一个C程序,其想要输出20个减号,不过,粗心的程序员把代码写错了,你需要把下面的代码修改正确,不过,你只能增加或是修改其中的一个字符,请你给出三种答案。
int n = 20;
for(int i = 0; i < n; i--){
print
}
?>
答案:
//错误答案
//第一种解法:在for循环中给n加一个负号
for(int i = 0; i < -n; i--)
//第二种解法:把 n 初始化成 -20
int n = -20;
//第三种解法:把for循环中的 i 初始化成40
for(int i = 40; i < n; i--)
//正确答案
//第一种解法:在for循环中给 i 加一个负号
for(int i = 0; -i < n; i--)
//第二种解法:在for循环中把 i-- 变成 n--
for(int i = 0; i < n; n--)
//第三种解法:把for循环中的 < 变成 +
for(int i = 0; i + n; i--)