小C拼图 [英] small C puzzles

查看:70
本文介绍了小C拼图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在浏览这个问题:


以下是一段C代码,其目的是打印一个

减号20次。但你可以注意到,它不起作用。


#include< stdio.h>

int main()

{

int i;

int n = 20;

for(i = 0; i< n; i-- )

printf(" - ");

返回0;

}


修好上面的代码很简单。要解决问题

有趣,你必须通过改变一个

字符来修复上面的代码。有三种已知的解决方案。看看你是否可以获得这三个人的全部金额。

一个解决方案是使用n--而不是i--。请让我知道

其他两个。

解决方案



< ma ** ***********@hotmail.com>在消息中写道

news:33 ************************* @ posting.google.co m ... < blockquote class =post_quotes>我正在浏览这个问题:

以下是一段C代码,其意图是
打印减号20次。但你可以注意到,它不起作用。

#include< stdio.h>
int main()
{
int i;
int n = 20;
for(i = 0; i< n; i--)
printf(" - ");
return 0;
}

修复上面的代码很简单。要使
问题变得有趣,您必须通过将
改为一个字符来修复上述代码。有三种已知的解决方案。看看你是否可以获得
这三个。

一个解决方案是使用n--而不是i--。请告诉我另外两个




当然....告诉明显的人......;)


一个是改变i< n到-i< n


使用〜我几乎可以工作 - 并且会在

的实现上使用一个人的赞美。


另一种是将测试改为i + n


好​​的小逻辑谜题。


在文章< 10 ************* @ corp.supernews.com> ;, wi *****@toomuchspam.net

说...


< ma ********** ***@hotmail.com>在消息中写道
新闻:33 ************************* @ posting.google.co m ...

我正在浏览这个问题:

以下是一段C代码,其意图是


打印

减号20次。但你可以注意到,它不起作用。

#include< stdio.h>
int main()
{
int i;
int n = 20;
for(i = 0; i< n; i--)
printf(" - ");
return 0;
}

修复上面的代码很简单。要使


问题

有趣,你必须修改上面的代码,通过改变


一个

字符。有三种已知的解决方案。看看你是否可以


所有

这三个。

一个解决方案是使用n--而不是i--。请告诉我


其他两个。



当然....告诉显而易见的人......;)

一个是改变我< n到-i< n

使用〜我几乎可以工作 - 并且会使用一个恭维的实现。

另一个是将测试更改为i + n

好的小逻辑谜题。




除了Dan在早先中指出的那样。同一件事的版本

它仍然保证在上面的形式工作。


-

兰迪霍华德

要回复,请删除FOOBAR。


>一种解决方案是使用n--而不是i--。请让我知道

其他两个。




还有一个:


for(i = 0;〜我< n; i--)


使用按位否定。


i was browsing this problem:

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;
}

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.
one solution is to use n-- instead of i--. please let me know the
other two.

解决方案


<ma*************@hotmail.com> wrote in message
news:33*************************@posting.google.co m...

i was browsing this problem:

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;
}

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.
one solution is to use n-- instead of i--. please let me know the other two.



Sure....Tell the obvious one...;)

One is to change i < n to -i < n

Using ~i will almost work - and would on an implementation that
uses one''s compliment.

The other is to change the test to i + n

Good little logic puzzle.



In article <10*************@corp.supernews.com>, wi*****@toomuchspam.net
says...


<ma*************@hotmail.com> wrote in message
news:33*************************@posting.google.co m...

i was browsing this problem:

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;
}

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.
one solution is to use n-- instead of i--. please let me know


the

other two.



Sure....Tell the obvious one...;)

One is to change i < n to -i < n

Using ~i will almost work - and would on an implementation that
uses one''s compliment.

The other is to change the test to i + n

Good little logic puzzle.



Except, as Dan pointed out in the "earlier" version of this same thing
it still is guaranteed to work in the form above.

--
Randy Howard
To reply, remove FOOBAR.


> one solution is to use n-- instead of i--. please let me know the

other two.



One more:

for (i = 0; ~i < n; i--)

Using bitwise negation.


这篇关于小C拼图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆