大米&棋盘。加倍每个方方面面的问题 [英] Rice & Chessboard. Doubling Each Square Problem

查看:76
本文介绍了大米&棋盘。加倍每个方方面面的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个简单的问题,但我只是将其包含在

标题中,因为它解释了我的程序尝试做什么。


以下不起作用,因某些原因不想将
的值放在新行上。

========== ======================================== ========== ===========

#include< stdio.h>

#include< conio.h>

int main(无效)

{

/ *可变延期* /

长i = 1,计数器= 1, tally = 0;


/ *计划正文* /

做{i = i * 2;

printf(" %d \t%d \ n",counter,i); / *< =======这里* /

++ counter;

}

而(counter!= 65) ;

getch();

}

==================== ============================== ==================== =


以下情况确实有效,但所有数字都排成长线

并且没有分开。以下工作:


================================= ================= =====================

#include< stdio.h>

#include< conio.h>


int main(无效)

{

/ *可变延期* /

long i = 1,counter = 1,tally = 0;


/ * Program Body * /

做{i = i * 2;

printf("%d",i); / *< =======这里* /

++ counter;

}

而(counter!= 65) ;

getch();

}

==================== ============================== ==================== =


我真的很想知道原因。出于好奇,据我所知,我似乎没有做任何错误的事情。谢谢。

-

回收你的收件箱! http://www.mozilla.org/products/thunderbird

I know this is a simple problem, but I''ve just included this in the
title because it explains what my program tries to do.

The following does not work, it for someone reason does not want to
place the values on a new line.
================================================== =====================
#include <stdio.h>
#include <conio.h>

int main(void)
{
/* Variable delcaration */
long i=1, counter=1, tally=0;

/* Program Body */
do {i = i * 2;
printf("%d \t %d \n", counter, i); /* <======= Here */
++counter;
}
while (counter != 65);
getch();
}
================================================== =====================

Whereas the following does work, but all the numbers are in a long line
and not separated. The following does work:

================================================== =====================
#include <stdio.h>
#include <conio.h>

int main(void)
{
/* Variable delcaration */
long i=1, counter=1, tally=0;

/* Program Body */
do {i = i * 2;
printf("%d", i); /* <======= Here */
++counter;
}
while (counter != 65);
getch();
}
================================================== =====================

I''d really like to know why. Out of curiosity, I don''t seem to be doing
anything wrong, to my knowledge. Thanks.
--
Reclaim Your Inbox! http://www.mozilla.org/products/thunderbird

推荐答案

在文章< ee ********** @ news.freedom2surf.net>,

Jonathan Pritchard< jo ********** @ f2s.comwrote:
In article <ee**********@news.freedom2surf.net>,
Jonathan Pritchard <jo**********@f2s.comwrote:

> #include< stdio.h>
#包括< conio.h>
>#include <stdio.h>
#include <conio.h>



< conio.hand getch()不是标准C的一部分。


这次我们可以找出问题而不看那些部分的b $ b。

<conio.hand getch() are not part of standard C.

It happens this time that we can identify the problem without looking
at those parts.


long i = 1,counter = 1,tally = 0;
long i=1, counter=1, tally=0;


printf("%d \ t%d \ n",counter,i); / *< =======这里* /
printf("%d \t %d \n", counter, i); /* <======= Here */



%d是int的格式,但不是很长的格式。你需要%ld

a长。

-

法律 - 它是商品

- Andrew Ryan(环球邮报,2005/11/26)

%d is the format for an int but not for a long. You need %ld for
a long.
--
"law -- it''s a commodity"
-- Andrew Ryan (The Globe and Mail, 2005/11/26)


Walter Roberson写道:
Walter Roberson wrote:

文章< ee ********** @ news.freedom2surf.net>,

Jonathan Pritchard< jo ********** @ f2s.comwrote:
In article <ee**********@news.freedom2surf.net>,
Jonathan Pritchard <jo**********@f2s.comwrote:

> #include< stdio.h>
#include< conio.h>
>#include <stdio.h>
#include <conio.h>



< conio.hand getch()不是标准C的一部分。


这次我们可以找出问题而不看那些部分的



<conio.hand getch() are not part of standard C.

It happens this time that we can identify the problem without looking
at those parts.


> long i = 1,counter = 1,tally = 0;
> long i=1, counter=1, tally=0;


> printf("%d \t%d \ n",counter,i); / *< =======这里* /
> printf("%d \t %d \n", counter, i); /* <======= Here */



%d是int的格式,但不是很长的格式。你需要%ld for



%d is the format for an int but not for a long. You need %ld for
a long.



那么为什么会抑制换行的打印?

So why would that suppress the printing of the newline?


Jonathan Pritchard写道:
Jonathan Pritchard wrote:

我知道这是一个简单的问题,但我只是将其包含在

标题中,因为它解释了我的程序尝试做什么。 br />

以下不起作用,因某些原因不想将
的值放在新行上。

= ================================================= = ====================

#include< stdio.h>

#include< conio。 h取代;
I know this is a simple problem, but I''ve just included this in the
title because it explains what my program tries to do.

The following does not work, it for someone reason does not want to
place the values on a new line.
================================================== =====================
#include <stdio.h>
#include <conio.h>



不要包含非标准标题。

有很多替代品可以在程序结束时使用getch()。 [不,我不会告诉你

你,

这就是谷歌的用途。 ; - ]

Don''t include non-standard headers. There are plenty of alternatives to
putting getch() at the end of the program. [No, I''m not going to tell
you,
that''s what Google is for. ;-]


int main(无效)

{

/ *可变延期* /
int main(void)
{
/* Variable delcaration */



我知道有些老师坚持要求学生在代码中加入这样的评论。

就个人而言,我认为他们是愚蠢的。

I know some teachers insist on students putting such comments in code.
Personally, I think they''re stupid.


long i = 1,counter = 1,tally = 0;


/ *程序正文* /

do {i = i * 2;

printf("%d \ t%d \ n",counter,i); / *< =======这里* /
long i=1, counter=1, tally=0;

/* Program Body */
do {i = i * 2;
printf("%d \t %d \n", counter, i); /* <======= Here */



您声明了计数器,我声明为_long_ int,但是您尝试将它们打印为

int使用

%d。要打印long int,您需要使用%ld。


在标签异常之前和之后打印额外空格。不是

错误,但

一般不是你想要做的。有一些方法可以制作printf

打印

固定宽度字段内的整数。 [RTFM]


请注意,当显示



时,可能会忽略换行前的额外空格(或管道输出)某些系统上的文件。

You declared counter and i as _long_ int, but you try to print them as
int using
%d. To print a long int you need to use %ld.

Printing additional space before and after a tab is unusual. Not an
error, but
generally not what you''ll want to do. There are ways of making printf
print
an integer within a fixed width field. [RTFM]

Note that the extra space before the newline may be ignored when the
line
is displayed (or piped to file) on some systems.


++ counter;

}

while(counter!= 65) );
++counter;
}
while (counter != 65);



for()循环更加惯用。

A for() loop would be more idiomatic.


getch();
getch();



更好只是......


返回0;

Better is just...

return 0;


}

...

以下可行,
}
...
Whereas the following does work,



不,它有同样的错误。未定义的行为可以做任何事情,

包括

给人的印象是程序有效。


< snip>

No, it has the same error. Undefined behaviour can do anything,
including
giving the impression that a program works.

<snip>


我真的很想知道原因。出于好奇,据我所知,我似乎没有做任何错误的事情。
I''d really like to know why. Out of curiosity, I don''t seem to be doing
anything wrong, to my knowledge.



扩展您的知识并阅读printf的规格。

该功能比它看起来更强大,更复杂。

很容易被误用。


-

彼得

Expand your knowledge and read the specification of printf.
The function is more powerful and complicated than it looks.
It''s easily misused.

--
Peter


这篇关于大米&amp;棋盘。加倍每个方方面面的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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