“火柴棍式”程序员面试题
来源: http://www.xymyeah.com/163.html
下面是一个C程序,其想要输出20个减号,不过,粗心的程序员把代码写错了,你需要把下面的代码修改正确,不过,你只能增加或是修改其中的一个字符,请你给出三种答案。
int n = 20; for(int i = 0; i < n; i--){ printf("-"); }
答案:
//第一种解法:在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--)
原文: http://www.xymyeah.com/163.html