无法解决有关此代码的难题
int i,n=20;
for(i=0;i<n;i--)
printf("-");
我绞尽脑汁却没能解决这个问题。
从上面的代码中删除任何单个字符或运算符,程序应该打印“-”20次,
请帮忙!
int i,n=20;
for(i=0;i<n;i--)
printf("-");
I have been rattling my brain but have not been able to solve this.
Delete any single character or operator from above code and the program should print "-" 20 times
Please help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我不认为你可以通过删除一个角色来做到这一点,但我有三种替换解决方案(好吧,其中一个添加了一个角色,但只是因为你有程序中没有空格。如果有空格,它将替换空格)。
解决方案1
解决方案2
解决方案3
I don't think you can do it by deleting a character, but I have three solutions that replace (well, one of them adds a character, but only because you have no whitespace in your program. If you had whitespace, it would replace a space).
Solution 1
Solution 2
Solution 3
我在 C Puzzles 上找到了对该问题的引用。 (这是在评论中,所以它肯定不是原始来源。)
请注意,说明说:
一种解决方案是在 for 循环头中将
i--
更改为n--
。I found a reference to the problem on C Puzzles. (It's in a comment, so it's surely not the original source.)
Note that the instructions say:
One solution is to change
i--
ton--
in the header of the for loop.如前所述,该问题没有解决方案。无论是你还是任何向你提出这个问题的人都错误地表述了这一问题。
The problem, as stated, has no solution. Either you or whoever gave that problem to you have stated it incorrectly.
我不知道替换是否可以,但将 i-- 更改为 n-- 应该可以解决问题
I don't know if replacing is ok but changing i-- to n-- should do the trick
该谜题应该允许“改变一个角色”。
解决方案是改变<改为 +,将 i 改为 n,或将 for 循环中间 i 之前的空格改为 - (应该有空格。)
你的朋友没有明白这个问题。 :-)
The puzzle is supposed to allow for "changing one character".
The solutions are to change < to +, change i to n, or change the space before i in the middle of the for loop to a - (there's supposed to be spaces.)
Your friend doesn't get the question. :-)
编辑:您使用的是减量运算符而不是增量。所以你希望它继续递增 i 直到达到 20。此时它将停止,因为那时 i 将不再小于 20 而是等于。
EDIT: You were using a decrement operator instead of increment. So you want it to keep incrementing i till it reaches 20. At which point it will stop because then i would no longer be less than 20 but equal to.
该程序已经打印了
-
20次——然后它继续打印更多次。该谜题并没有说需要将其打印恰好 20 次。如果您确实必须删除某些内容,那么您可以通过删除
--
运算符来获得类似的行为。其他可供删除的候选字符是换行符。
The program already prints
-
20 times — and then it continues to print it a lot more afterward. The puzzle didn't say it needed to print it exactly 20 times.If you really must delete something, then you can get similar behavior by deleting the
--
operator.Other characters that are candidates for deletion are the line breaks.
我可以通过添加单个字符来做到这一点:
I can do it by adding a single character:
更改
为:
但我不明白如何只能删除字符或运算符...您必须修改运算符或字符。
Change:
to:
But I don't see how you can only delete a char or operator... You have to modify an operator or character.
我已经好几年没有做过c了,我很迂腐,所以请原谅我,但是……程序不是已经打印了20次“-”了吗?然后还有一些?
如果把“printf”中的“f”删掉,不是会继续打印“-”20次吗?至少?
如果这是一个技巧问题,也许这就是技巧......
It's been years since I did c, and I'm pedantic, so please forgive me, but... doesn't the program print "-" 20 times already? And then some?
If you delete the "f" from "printf", doesn't it continue to print "-" 20 times? At least?
If it's a trick question, maybe this is the trick...