一些C难题 [英] some C puzzles

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

问题描述

我正在阅读以下代码:


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

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


#include< stdio.h>

int main()

{

int i;

int n = 20;

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

printf(" - ");

返回0;

}


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

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

字符来修复上面的代码。有三种已知的解决方案。看看你能不能得到所有这两个



一个sol是使用n--而不是i,所以它会打印减号20

次。

I was going through the following code:

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 sol is to use n-- instead of i, so it will print minus sign 20
times.

推荐答案

在< 33 ***************** ********@posting.google.com> ma*************@hotmail.com 写道:
In <33*************************@posting.google.com> ma*************@hotmail.com writes:
我正在阅读以下代码:

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

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

修复上面的代码很简单。为了使问题变得有趣,你必须通过改变一个
字符来修复上面的代码。
I was going through the following code:

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.




这是不可能的,在一般情况下,因为代码必须在最后一个减号之后输出换行符

字符(否则它可能根本不会产生任何

输出),这不能通过完全改变来实现一个

字符。


Dan

-

Dan Pop

DESY Zeuthen,RZ集团

电子邮件: Da*****@ifh.de


1。我们可以改变i--到i ++

2.我们可以改变i--到n--

3. ??
Da ***** @ cern.ch (Dan Pop)在留言中写道:< ch *********** @ sunnews.cern.ch> ...
1. we can change i-- to i++
2. we can change i-- to n--
3. ??
Da*****@cern.ch (Dan Pop) wrote in message news:<ch***********@sunnews.cern.ch>...
< 33 ************************* @ posted。 google.com> ma*************@hotmail.com 写道:
In <33*************************@posting.google.com> ma*************@hotmail.com writes:
我正在阅读以下代码:

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

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

修复上面的代码很简单。为了使问题变得有趣,你必须通过改变一个
字符来修复上面的代码。
I was going through the following code:

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.



这通常是不可能的,因为代码必须在最后一个减号之后输出换行符(否则它可能根本不会产生任何
输出),这不能通过改变一个
字符来实现。

Dan



This is not possible, in general, because the code must output a newline
character after the last minus sign (otherwise it might not produce any
output at all) and this cannot be achieved by changing exactly one
character.

Dan



请不要发帖。查看最后一天左右的帖子

反对它的论据。


2004年9月2日01:21:16 -0700 am****@yahoo.com (Ash)写道:
Please don''t top post. Look at the posts within the last day or so for
the arguments against it.

On 2 Sep 2004 01:21:16 -0700
am****@yahoo.com (Ash) wrote:
< a href =mailto:Da ***** @ cern.ch> Da ***** @ cern.ch (Dan Pop)在留言中写道
新闻:< ch ** ********* @ sunnews.cern.ch> ...
Da*****@cern.ch (Dan Pop) wrote in message
news:<ch***********@sunnews.cern.ch>...
< 33 ******************* ******@posting.google.com>
ma ***** ********@hotmail.com 写道:
In <33*************************@posting.google.com>
ma*************@hotmail.com writes:
我正在阅读以下代码:

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

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

修复上面的代码很简单。为了使问题变得有趣,你必须通过改变一个字符来修复上面的代码。
这一般是不可能的,因为代码必须输出一个
最后一个减号之后的换行符(否则它可能根本不会产生任何输出),这不能通过改变一个字符来实现。
I was going through the following code:

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.
This is not possible, in general, because the code must output a
newline character after the last minus sign (otherwise it might not
produce any output at all) and this cannot be achieved by changing
exactly one character.



1.我们可以改变i--到i ++


这改变了2个字符。

2.我们可以改变i--到n- -
3. ??


1. we can change i-- to i++
That is changing 2 characters.
2. we can change i-- to n--
3. ??






程序终止之前,您的任何更改都不会导致输出新行。正如Dan正确地指出的那样(并且他通常是正确的,即使有些人发现他很有吸引力),如果他们希望最后一行能够保证,那么在
结束之前,程序必须输出换行符。输出。

-

Flash Gordon

有时我觉得拍摄对某些人来说太好了。

虽然我的电子邮件地址是垃圾邮件,但它是真实的,我读了它。



Neither of your changes causes a new line to be output before the
program terminates. As Dan correctly stated (and he is generally correct
even if some find him abrasive) programs must output a newline before
terminating if they want the last line to be guaranteed to be output.
--
Flash Gordon
Sometimes I think shooting would be far too good for some people.
Although my email address says spam, it is real and I read it.


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

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