通过改变一个字符修复一个破碎的循环 [英] Fixing a broken loop by changing exactly one character
问题描述
我发现网站与一些复杂的难题Ç的。现在我正在处理这个:
I found a site with some complicated C puzzles. Right now I'm dealing with this:
下面是一个件C code,其意图是要打印减号20倍。但是你可以发现,这是行不通的。
The following is a piece of C code, whose intention was to print a minus sign 20 times. But you can notice that, it doesn't work.
#include <stdio.h>
int main()
{
int i;
int n = 20;
for( i = 0; i < n; i-- )
printf("-");
return 0;
}
好固定高于code是直线前进。为了使问题有意思,你得解决上述code,通过改变一个字符。有三种已知的解决方案。看看你是否可以得到所有这三个。
Well fixing the above code is straight-forward. To make the problem interesting, you have to fix the above code, by changing exactly one character. There are three known solutions. See if you can get all those three.
我无法弄清楚如何解决。我知道,它可以通过改变是固定的 -
到 ++
,但我不能找出单一性格改变,使其工作。
I cannot figure out how to solve. I know that it can be fixed by changing --
to ++
, but I can't figure out what single character to change to make it work.
推荐答案
下面是一个解决办法:
for( i = 0; -i < n; i-- )
printf("-");
这是第二个,这要归功于马克对我的帮助!
Here is a second one, thanks to Mark for helping me!
for( i = 0; i + n; i-- )
printf("-");
和马克也有第三个是
for( i = 0; i < n; n-- )
printf("-");
这篇关于通过改变一个字符修复一个破碎的循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!